【问题标题】:How do you add a new MediaType to the MediaTypeFormatters used by ASP.Net WebApi?如何将新的 MediaType 添加到 ASP.Net WebApi 使用的 MediaTypeFormatters?
【发布时间】:2014-04-11 21:43:42
【问题描述】:
我有一个 ASP.Net WebAPI 项目将返回 JSON,但特别是 HAL+JSON。我知道我可以编写一个自定义 MediaTypeFormatter 来执行此操作,但我计划返回相同的 HAL 格式响应,即使您要求纯 JSON,所以我的问题是:
除了为 HAL+JSON 编写新的 MediaTypeFormatter 之外,我是否可以将新的媒体类型(“application/hal+json”)添加到默认 WebAPI JsonMediaTypeFormatter 识别的媒体类型列表中?
【问题讨论】:
标签:
c#
asp.net
json
asp.net-web-api
【解决方案1】:
我想我找到了答案。在 Global.asax:Application_Start() 中,我可以这样做:
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/hal+json"));