2017-08-05 23 views
1

一个供应商的API我需要使用在发送与内容类型的POST请求:文本/无格式和JSON在体内。.NET 1.0核心网络API处理请求体作为JSON当内容类型是文本/无格式

如何解析它在.NET 1.0核心API的网站?

我敢肯定,我需要做类似this(下面的代码)回答的东西,但我不知道如何在网页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; 
      } 
     } 
    } 

回答

2

我把它加入了text/plain内容类型的JsonInputFormatterStartup.csConfigureServices()方法,像这样的工作:

 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")); 
       } 
      }); 
      ... 
     } 

编辑:我做了一个seed项目SNS客户机拉姆达,它解析该消息并确认认购开箱。

相关问题