如何在不更改全局配置的情况下强制asp.net web api 2响应xml

如何在不更改全局配置的情况下强制asp.net web api 2响应xml,asp.net,asp.net-web-api,Asp.net,Asp.net Web Api,我的网站很有用 config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html")); 强制响应json 但是有一些特定的api需要返回xml格式的数据 我用 public HttpResponseMessage PostNotify(HttpRequestMessage request) { HttpResponseMessage resp = Request.

我的网站很有用

    config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
强制响应json

但是有一些特定的api需要返回xml格式的数据

我用

public HttpResponseMessage PostNotify(HttpRequestMessage request)
{
  HttpResponseMessage resp = Request.CreateResponse<PackageModal>(HttpStatusCode.OK, value: package,
                        formatter: Configuration.Formatters.XmlFormatter);
                    return resp;
}
公共HttpResponseMessagePostNotify(HttpRequestMessage请求) { HttpResponseMessage resp=Request.CreateResponse(HttpStatusCode.OK,值:package, 格式化程序:Configuration.Formatters.XmlFormatter); 返回响应; }
但它不起作用。仍然会得到json格式的响应。

在请求中使用accept标头,contentnegotiation将处理输出格式

您的ajax调用看起来像什么?我的回答有帮助吗?如果是,请接受。如果没有,请更详细地解释您的问题:)