Change Content-Type header for HttpResponseMessage

2019-08-24 03:32发布

问题:

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?