3

In order to make the model binder correctly bind the body part of an HTTP request in ASP.Net Core you need to use the [FromBody] attribute.

public JsonResult PostContent([FromBody] Content content)
{
    ......
}

This works great if your media type is application/json. However, if you want to use a vendor specific (e.g. application/vnd+mycompany+json) media type in your Accept header then if I pass that in the HTTP POST I will get a 415 Unsupported Media Type.

So my question is how do you support vendor specific media types in ASP.Net Core?

1

1 Answer 1

1

This is how I did it.

In Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddTransient<IApiInfoService, ApiInfoService>();
    services.AddTransient<IApiVersion, ApiVersion>();
    services.AddTransient<IContentService, ContentService>();
    services.AddTransient<IIdGenerator, GuidIdGenerator>();

    // Add framework services.
    services.AddMvc(
        mvcConfig => {
            mvcConfig.InputFormatters.OfType<JsonInputFormatter>().First().SupportedMediaTypes.Add(
                MediaTypeHeaderValue.Parse(ContentTypes.VENDOR_MIME_TYPE)
            );
        }
    );
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.