How can I set a default content-type of text/html

2019-04-09 18:25发布

问题:

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.

回答1:

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.



回答2:

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

Content-Type: text/html; charset=utf-8


回答3:

"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.



回答4:

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