首页 > 解决方案 > 带有接受标头和 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 的行为。

我只是不确定如何在支持的媒体类型上正确匹配,或者是否有更好、更简单的解决方案......

标签: asp.net-web-apiasp.net-web-api2content-negotiation

解决方案


我确定使用自定义内容协商器是一个红鲱鱼。相反,我能够使用与特定 url 参数匹配的 MediaTypeMapping 而不是接受请求标头:

fmtJson.MediaTypeMappings.Add(new System.Net.Http.Formatting.QueryStringMapping("_format", "json", "application/json"));

推荐阅读