asp.net-web-api - 带有接受标头和 url 参数的 Web API 内容协商格式化程序
问题描述
我已经实现了内容协商,以便根据接受标头使用特定的序列化程序:
XmlFormatter fmtXml = new XmlFormatter();
fmtXml.SupportedMediaTypes.Add(new
System.Net.Http.Headers.MediaTypeHeaderValue("text/xml"));
JsonFormatter fmtJson = new JsonFormatter();
fmtJson.SupportedMediaTypes.Add(new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"));
config.Formatters.Insert(0, fmtJson);
config.Formatters.Insert(0, fmtXml);
我需要允许客户端使用 url 参数指定所需的格式,该参数优先于接受标头。
为此,我开始继承 DefaultContentNegogiator (虽然我不知道这是最好的主意。:
public class CustomContentNegotiator : DefaultContentNegotiator
{
public override ContentNegotiationResult Negotiate(Type type, HttpRequestMessage request, IEnumerable<MediaTypeFormatter> formatters)
{
string sMimeType = HttpUtility.ParseQueryString(request.Url.Query).Get("_format");
if (!string.IsNullOrEmpty(sMimeType))
{
...
}
else
{
return base.Negotiate(type, request, formatters);
}
}
}
然后我用我的替换默认的内容协商器:
GlobalConfiguration.Configuration.Services.Replace(typeof(IContentNegotiator), new CustomContentNegotiator());
自定义内容协商器的想法是,如果已将内容格式指定为 url 参数,我将找到匹配的格式化程序,否则我将回退到 DefaultContentNegotiator 的行为。
我只是不确定如何在支持的媒体类型上正确匹配,或者是否有更好、更简单的解决方案......
解决方案
我确定使用自定义内容协商器是一个红鲱鱼。相反,我能够使用与特定 url 参数匹配的 MediaTypeMapping 而不是接受请求标头:
fmtJson.MediaTypeMappings.Add(new System.Net.Http.Formatting.QueryStringMapping("_format", "json", "application/json"));
推荐阅读
- javascript - 如何在 Chrome 扩展的 Active Tab 的两个页面上运行后台 JS
- c3.js - 基于 C3js 圆环图数据的条件颜色
- java - 哪个 JAR 用于 JSTL?
- ubuntu - 在训练模型时运行应用程序
- messaging - 基于事件的系统中的消息格式
- php - AWS SQS 错误:SignatureDoesNotMatch HTTP 错误:403
- magento2 - 无法使用 magento 2 REST API 取消订单
- google-analytics - 如何将 Google Analytics 虚拟网页浏览量与事件关联起来
- python - 按特定类属性对列表中的 Python 对象进行排序
- javascript - (JavaScript) 对 JSON 散列进行排序,但不基于散列键