views:

528

answers:

4

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.

+4  A: 

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.

swilliams
Another issue is that some people spoof user agents.
womp
Definitely, but unless there's another way...
swilliams
The thing is, IE supports inline SVG. I have that working using the AdobeSVG. The only thing I need is for the save view to have two different content-types.
John Gietzen
+2  A: 

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.

TJB
+1  A: 

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.

Daniel Schaffer
Ok, thank you. I realize all of this. However, if I use a content type OTHER THAN text/html, IE WILL NOT RENDER IT.
John Gietzen
No need to get snippy. As far as I can tell, IE does in fact render XHTML correctly when using the application/xhtml+xml content type. Can you post an example in your question of the content that is not rendering correctly?
Daniel Schaffer
The problem I run into is when I try to use a XHTML doctype, the MSXML parser ends up throwing an error reading the document, citing an issue with the order that entities are declared in the doctype.
John Gietzen
A: 

Kind of hacky, but... What if the SVG was in a frame, the frame advertised content-type=text/xml, while the containing page advertsized the more proper application/xhtml+xml. This divides the problem into two (possibly) more tractable ones.

codingoutloud
That is actually pretty close to what I actually did. However, I used an `object` instead of an `iframe`. Check it out: http://lanlordz.net/Events/ViewTournament/2
John Gietzen
Actually, that tournament never had a bracket. Try this one instead: http://lanlordz.net/Events/ViewTournament/4
John Gietzen