将MediaType添加到现有JsonInputFormatter

Kyl*_*yle 7 asp.net-core-mvc

我在asp.net核心mvc中写了一个webhook,调用者发布了一些json.但Content-Type设置为application/vnd.myget.webhooks.v1+json.我只想把这个内容类型映射到JsonInputFormatter.

我这样做了,但想知道是否有更好的方法:

services.AddMvc( mvcConfig =>
{
    var formatter = new JsonInputFormatter();
    formatter.SupportedMediaTypes.Add( 
         new MediaTypeHeaderValue("application/vnd.myget.webhooks.v1+json") );
    mvcConfig.InputFormatters.Add( formatter );
});
Run Code Online (Sandbox Code Playgroud)

KCD*_*KCD 6

您可以修改默认InputFormatterConfigureServices

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

......也许是略有改善