简体   繁体   English

如何向 ASP.Net WebApi 使用的 MediaTypeFormatters 添加新的 MediaType?

[英]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.我有一个 ASP.Net WebAPI 项目,它将返回 JSON,但特别是 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:我知道我可以编写自定义 MediaTypeFormatter 来执行此操作,但是即使您要求使用纯 JSON,我也计划返回相同的 HAL 格式的响应,所以我的问题是:

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?与其为 HAL+JSON 编写新的 MediaTypeFormatter,我是否可以将新的媒体类型(“application/hal+json”)添加到默认的 WebAPI JsonMediaTypeFormatter 识别的媒体类型列表中?

Think I found my answer.想我找到了我的答案。 In Global.asax:Application_Start(), I can do this:在 Global.asax:Application_Start() 中,我可以这样做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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