问题是is
Content-Type是
application/json,而请求有效载荷实际上是
text/plain。这将导致415不支持的媒体类型HTTP错误。
您至少有两个选项可以使
Content-Type内容与实际内容对齐。
使用application / json
保留
Content-Typeas
application/json并确保请求有效负载是有效的JSON。例如,使您的请求有效负载为:
{ "cookie": "=sec_session_id=[redacted]; _ga=[redacted]; AWSELB=[redacted]"}然后,动作签名需要接受与JSON对象具有相同形状的对象。
public class cookieWrapper{ public string cookie { get; set; }}代替
cookieWrapper类,或者您可以接受动态或a
Dictionary<string,string>并像
cookie["cookie"]在端点中那样对其进行访问
public IActionResult GetRankings([FromBody] cookieWrapper cookie)public IActionResult GetRankings([FromBody] dynamic cookie)public IActionResult GetRankings([FromBody] Dictionary<string, string> cookie)
使用文字/纯文字
另一种选择是将项目更改
Content-Type为
text/plain,并向项目中添加纯文本输入格式器。为此,请创建以下类。
public class TextPlainInputFormatter : TextInputFormatter{ public TextPlainInputFormatter() { SupportedMediaTypes.Add("text/plain"); SupportedEncodings.Add(UTF8EncodingWithoutBOM); SupportedEncodings.Add(UTF16EncodingLittleEndian); } protected override bool CanReadType(Type type) { return type == typeof(string); } public override async Task<InputFormatterResult> ReadRequestBodyAsync( InputFormatterContext context, Encoding encoding) { string data = null; using (var streamReader = context.ReaderFactory( context.HttpContext.Request.Body, encoding)) { data = await streamReader.ReadToEndAsync(); } return InputFormatterResult.Success(data); }}并配置Mvc以使用它。
services.AddMvc(options =>{ options.InputFormatters.Add(new TextPlainInputFormatter());});也可以看看
https://github.com/aspnet/Mvc/issues/5137



