Add MediaType to existing JsonInputFormatter

I am writing a webhook in asp.net core mvc where the caller sends a json message. But Content-Type is set to application/vnd.myget.webhooks.v1+json . I just want this content type map to be in JsonInputFormatter .

I did this, but I wonder 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 ); }); 
+5
source share
1 answer

You can change the default value of 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

+3
source

Source: https://habr.com/ru/post/1247756/


All Articles