c# - 如何将新的 MediaType 添加到 ASP.Net WebApi 使用的 MediaTypeFormatters?

标签 c# asp.net json asp.net-web-api

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

我可以将新媒体类型(“application/hal+json”)添加到默认 WebAPI JsonMediaTypeFormatter 可识别的媒体类型列表中,而不是为 HAL+JSON 编写新的 MediaTypeFormatter 吗?

最佳答案

我想我找到了答案。在 Global.asax:Application_Start() 中,我可以这样做:

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

关于c# - 如何将新的 MediaType 添加到 ASP.Net WebApi 使用的 MediaTypeFormatters?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22996964/

相关文章:

c# - 我应何时保存 Windows Phone 7 上的设置?

asp.net - 命名 CSS 选择器的正确方法是什么。它的工作方式似乎不一致

asp.net - 一种#if(调试符号),但用于 web.config 文件

javascript - 如何将 JSON 数组集成到 JavaScript 文件中?

c# - Newtonsoft.Json : DeserializeObject(string value, 类型 type) 返回 JObject

c# - 列出 MyDocuments 目录文件奇怪的输出

c# - 使用 List<string> 类型作为 DataRow 参数

asp.net - 网站首次访问时启动时间异常长(总共长达 68 秒)

java - Spring Boot 自动 JSON 到 Controller 中的对象

c# new thread from timer handle 问题