我需要使用的供应商 API 正在发送内容类型为 text/plain 且正文中为 JSON 的 POST 请求。
如何在.net core 1.0 web api中解析它?
我确定我需要做类似的事情this(下面的代码)答案,但我不知道如何在 web api 中。
public class RawContentTypeMapper : WebContentTypeMapper
{
public override WebContentFormat GetMessageFormatForContentType(string contentType)
{
switch (contentType.ToLowerInvariant())
{
case "text/plain":
case "application/json":
return WebContentFormat.Json;
case "application/xml":
return WebContentFormat.Xml;
default:
return WebContentFormat.Default;
}
}
}
我通过添加使其工作text/plain
内容类型为JsonInputFormatter
in Startup.cs
ConfigureServices()
方法,像这样:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(config =>
{
foreach (var formatter in config.InputFormatters)
{
if (formatter.GetType() == typeof(JsonInputFormatter))
((JsonInputFormatter)formatter).SupportedMediaTypes.Add(
MediaTypeHeaderValue.Parse("text/plain"));
}
});
...
}
Edit: 我制造了一个seedSNS 客户端 lambda 的项目,它解析消息并立即确认订阅。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)