当内容类型为text/plain时,.NET Core 1.0 Web Api将请求正文作为JSON处理

当内容类型为text/plain时,.NET Core 1.0 Web Api将请求正文作为JSON处理,json,.net-core,content-type,asp.net-core-webapi,Json,.net Core,Content Type,Asp.net Core Webapi,我需要使用的一个供应商API正在发送一个POST请求,其内容类型为:text/plain,正文中为JSON 如何在.NETCore1.0WebAPI中解析它 我确信我需要做一些类似于(下面的代码)答案的事情,但我不知道如何在WebAPI中实现 public class RawContentTypeMapper : WebContentTypeMapper { public override WebContentFormat GetMessageFormatForC

我需要使用的一个供应商API正在发送一个POST请求,其内容类型为:text/plain,正文中为JSON

如何在.NETCore1.0WebAPI中解析它

我确信我需要做一些类似于(下面的代码)答案的事情,但我不知道如何在WebAPI中实现

    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
内容类型添加到
Startup.cs
ConfigureServices()
方法中的
JsonInputFormatter
来实现这一点,如下所示:

        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"));
                }
            });
            ...
         }
编辑:我为SNS客户端lambda制作了一个项目,该项目将解析消息并即时确认订阅