Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add MediaType to existing JsonInputFormatter

I'm writing a webhook in asp.net core mvc where the caller posts some json. But the Content-Type is set to application/vnd.myget.webhooks.v1+json. I just want to have this content type map to the JsonInputFormatter.

I did this, but wondering if there is a better way:

services.AddMvc( mvcConfig =>
{
    var formatter = new JsonInputFormatter();
    formatter.SupportedMediaTypes.Add( 
         new MediaTypeHeaderValue("application/vnd.myget.webhooks.v1+json") );
    mvcConfig.InputFormatters.Add( formatter );
});
like image 513
Kyle Avatar asked Apr 22 '16 21:04

Kyle


1 Answers

You can modify the default InputFormatter in ConfigureServices

services.Configure<MvcOptions>(options => {
    options.InputFormatters.OfType<JsonInputFormatter>().First().SupportedMediaTypes.Add(
        new MediaTypeHeaderValue("application/vnd.myget.webhooks.v1+json")
    );
});

...perhaps a slight improvement

like image 181
KCD Avatar answered Oct 11 '22 13:10

KCD