views:

98

answers:

4

I'd like to set the default content-type for web pages in my ASP.NET MVC application to text/html.

I know this can be done by adding a ContentType="text/html" to all of my <%Page%> elements, but I'd prefer to use the web.config instead. How can I do that?

Thanks,

Adrian

Edit: I know that "text/HTML" is the ASP.NET default, but for unknown reasons Opera still tries to parse my web site as XHML unless I explicitly set the content-type in my <%Page%> element.

A: 

You could do this programatically in Global.asax.cs in the Global_BeginRequest event handler:

protected void Application_BeginRequest(Object sender, EventArgs e)
{
    HttpContext.Current.Response.ContentType = "text/html";
}

FYI, the docs say that "text/HTML" is the ASP.NET default anyway: http://msdn.microsoft.com/en-us/library/ms525208(VS.85).aspx

David
A: 

Content type for aspx views is already set to text/html.

Content-Type: text/html; charset=utf-8
Darin Dimitrov
I would have thought so too, but it does not seem to work properly for Opera. Please see my edit above.
Adrian Grigore
A: 

"text/html" is the default value for this property in the HttpResponse object.

Unless you need to set it to something else, do nothing. To set it to some other default, you might need to create a base Page class setting the property and inherit from this.

HectorMac
+2  A: 

We had the exact same problem and the issue for us was that the mobile.browser file we got from CodePlex has a bug that forces asp.net to always tell the desktop version of opera that we are sending them xhtml. I removed the mobile.browser file and it resolved the issue. It seems the only thing we could find that would override the directive from the mobile.browser was to specify the ContentType="text/html" on each views <%@ Page %> tag. Even setting the content type in global.asax made zero difference.

UPDATE: I have found that removing all of capability nodes with a name of "preferredRenderingMime" from the mobile.browser file will fix this issues and still allows us to identify mobile browsers.

Blegger
Yup, that's exactly the problem I am experiencing. As soon as I just removed this file, the standard content type was set to text/html again. Thanks a lot for your hint! I wonder why this was voted down?
Adrian Grigore