Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC: How do I send "text/xml" to all browsers but IE?

I need to be able to send the content type "text/xml" to Firefox and Safari, to allow them to render inline SVG in one of my pages.

This works, as long as the content type is "text/xml".

However, when IE hits the page, if the content type is not "text/html" it tries to render the XML document tree, rather than the XHTML content of the page.

What is the "right way" in ASP.NET MVC to set the HTTP Content-Type of ALL of my views?

Keep in mind that I am going to be rendering the views as ViewResults.

Ok, to clear any confusion up:

User Agent    Content-Type Desired
-----------------------------------
IE 5.5        text/html
IE 6          text/html
IE 7          text/html
IE 8          text/html
Firefox       text/xml
Safari        text/xml
Chrome        text/xml

And so on.

All of the browsers listed support SVG inline in some way or another. I have a consistent delivery format, save the content type.

like image 603
John Gietzen Avatar asked Aug 31 '09 20:08

John Gietzen


3 Answers

You could look at the properties in Request.Browser and sniff out IE that way, and return the proper view that way, though that is prone to issues. This isn't optimal because IE might support it in the future.

public ActionResult MyAction() {
  if (this.Request.Browser.Browser == "IE") {
    return View("NonSVG");
  } else {
    return View("SVG");
  }
}

Something worth looking into a little more might be this page on Codeplex. They define a property on Browser called AcceptsImageSVG, but it looks like it's geared towards mobile browsers, don't know if it could be used in your situation.

like image 180
swilliams Avatar answered Nov 20 '22 03:11

swilliams


According to W3, you should be using application/xhtml+xml rather than text/xml to signify XHTML:

http://www.w3.org/TR/2002/NOTE-xhtml-media-types-20020801/#text-xml

The above article also notes that text/html should not be used for XHTML content.

like image 36
Daniel Schaffer Avatar answered Nov 20 '22 04:11

Daniel Schaffer


You can determine the browser type by using the Request.Browser object

See this example http://msdn.microsoft.com/en-us/library/system.web.configuration.httpcapabilitiesbase.type%28VS.80%29.aspx

So you could do something like:

if( Request.Browser.Type.ToUpper().Contains("IE") )
{
  // Return IE View
}
else
{
  // Return the other view
}

Or, if you use this in lots of places you could create a ViewResult factory that returns the proper view result based on the browser type.

like image 2
TJB Avatar answered Nov 20 '22 03:11

TJB