.NET Core 1.0 Web Api treats request body as JSON when content type is text / plain

The provider API I need to use sends a POST request with the content type: text / plain and JSON in the body.

How to parse it in .net core 1.0 web api?

I am sure that I need to do something similar to this (code below), but I do not know how in the 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;
            }
        }
    }
+1
source share
1 answer

I did this by adding type text/plainto the method JsonInputFormatterin Startup.cs ConfigureServices(), for example:

        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, .

+3

All Articles