简体   繁体   中英

How do you add a new MediaType to the MediaTypeFormatters used by ASP.Net WebApi?

I have an ASP.Net WebAPI project that will be returning JSON, but specifically HAL+JSON. I know I can write a custom MediaTypeFormatter to do this, but I plan on returning the same HAL formatted response even if you ask for plain JSON, so my question is:

Rather than writing a new MediaTypeFormatter for HAL+JSON, can I add a new media type ("application/hal+json") to the list of media types that the default WebAPI JsonMediaTypeFormatter recognizes?

Think I found my answer. In Global.asax:Application_Start(), I can do this:

GlobalConfiguration.Configuration.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/hal+json"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM