Change Content-Type header for HttpResponseMessage

雨燕双飞 提交于 2020-01-06 20:15:16

问题


I am returning a crossdomain.xml file from WeB APi 2, and I need the Content-Type header to be "application/xml" or "text/xml". This is my controller:

public class CrossDomainController : ApiController
{
    public HttpResponseMessage Get()
    {
        var xmlString = File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + "crossdomain.xml");
        var result = Request.CreateResponse(HttpStatusCode.OK);
        result.Content = new StringContent(xmlString, Encoding.UTF8, "text/xml");
        return result;
    }
}

I have also added a route to WebApiConfig:

config.Routes.MapHttpRoute(
            "CrossDomain", "crossdomain.xml",
            new { controller = "CrossDomain" });

and a handler to Web.config:

  <add name="XMLHandler" type="System.Web.StaticFileHandler" path="*.xml" verb="GET"  />

The problem is, when I call this api the Content-Type header is always application/octet-stream.

How can I override this?

来源:https://stackoverflow.com/questions/35361075/change-content-type-header-for-httpresponsemessage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!