0

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?

Dan O'Leary
  • 2,660
  • 6
  • 24
  • 50
  • This may help - [http://stackoverflow.com/questions/11662028/webapi-to-return-xml](http://stackoverflow.com/questions/11662028/webapi-to-return-xml) – Teodor Kurtev Feb 12 '16 at 11:13
  • 2
    try result.Content.Headers.ContentType = new MediaTypeHeaderValue("text/xml"); – Piotr Czarnecki Feb 12 '16 at 11:16
  • Thanks, but neither of those have worked. I have also added a route to the WebApiConfig, and an xml handler to my webconfig (code for both above). Does that change anything? – Dan O'Leary Feb 12 '16 at 11:26
  • 1
    And are you sure you're serving the file via web api (if you put there breakpoint, is it hit ?) It can be that static file handler is serving this file instead (and in that case it depends on what you have as mimetype for .xml). Also if I were you I'd keep the duty of serving static file on static file handler - no need to keep web api engine in loop for that. – Ondrej Svejdar Feb 12 '16 at 11:26
  • Yep, it wasn't even hitting the controller. I've added to my web.config, and removed the controller and route. Everything is working perfectly now, thanks very much! – Dan O'Leary Feb 12 '16 at 11:57

0 Answers0