XHTML and MIME types-use web standards to generate ASP. NET 2.0 web sites ~

Source: Internet
Author: User
Tags xslt
When a web browser requests a page from a Web server, the web server assigns a specific MIME type (also known as the content type) to the page ). For example, an HTML page is assigned the text/html MIME type, a GIF image is assigned the image/gif mime type, and a Microsoft Word document is assigned the application/MSWord MIME type.

The browser uses the MIME type to determine how pages (or other resources) are processed ). For example, if a browser obtains a file with a recognizable image MIME type from a Web server, the browser tries to interpret the file and render it as an image. If the browser obtains a file with the application/MSWord MIME type, the browser may automatically open Microsoft Word to display this document (the exact behavior here depends on the browser and Its configuration method ).

W3C introduces a MIME type for the XHTML document. The new MIME type is application/XHTML + XML. W3C recommends that you use the application/XHTML + xml mime type when providing XHTML documents, because XHTML pages should be interpreted in a more rigorous way than legacy HTML pages.

Assign a specific MIME type to the ASP. NET page by including the contenttype attribute in the page command. For example, if the following command is included at the top of an ASP. NET page, application/XHTML + XML type will be assigned to the page.

Program code <% @ contenttype = "application/XHTML + XML" %>

W3C recommendation standards have a major problem: not all browsers can recognize application/XHTML + XML. In particular, Internet Explorer (the most popular Web browser ever) cannot recognize the application/XHTML + xml mime type. Therefore, using the recommended application/XHTML + xml mime type to provide XHTML pages is not a feasible choice.

There are three ways to solve this problem. You can use the text/html MIME type to provide XHTML pages, or use the application/XML (or text/XML) MIME type to provide XHTML pages, or use the content negotiation method. Let's discuss each of the above options.

The first option-provide pages as text/html-is the easiest option. By default, ASP. NET pages are assigned the MIME type. Better practice is to use this option when providing pages to an existing HTML browser, as per W3C recommendations (see http://www.w3.org/TR/xhtml-media-types ). If you create an XHTML 1.0 transitional page and the Web ApplicationProgramThe main audience uses browsers that do not understand the application/XHTML + xml mime type. Therefore, it is wise to provide pages in the text/html type. After all, the introduction of the XHTML 1.0 Transitional Standard aims to make it easier for developers to migrate existing HTML pages to XHTML.

This claim is controversial. For example, Ian Hickson believes that XHTML pages should never be provided as text/html, because this will lead to casual, nonstandard XHTML pages (see http://hixie.ch/advocacy/xhtml ). He suggested that the authors continue to use HTML 4.0 until more browsers fully support the XHTML standard.

The second option is to use the application/XML or text/xml mime type to provide the XHTML page as the XML type. When an XML document is provided to Internet Explorer, it is analyzed and presented as an XML document in the browser. (This document is represented by the xml dom exposed by the document. xmldocument object .)

The advantage of providing XHTML documents as XML is that any problems with the XHTML documents will be captured by the XML Analyzer of Internet Explorer. For example, if a document contains overlapping tags, or if the attribute value is not enclosed in quotation marks, the document is not displayed and an error message is displayed (see figure 4 ). The XHTML purist sees this behavior as a good thing because it prevents you from writing incorrectly formatted XHTML.

Figure 4. display XML in Internet Explorer

The problem with this method is: by default, Internet Explorer displaysSource code. Therefore, if the XHTML document is provided as XML, the Web site visitor will see the source of the XHTML documentCodeInstead of the expected output. W3C recommends a tip to solve this problem (see the http://www.w3.org/MarkUp/2004/xhtml-faq#ie): If you convert An XHTML document to HTML by using XSLT conversion, the document is analyzed as XML and displayed as HTML.

For example, the ASP. NET page in Listing 1 is provided as an XML document, but is converted to an HTML document. The results page is correctly displayed in Internet Explorer, opera, and Firefox.

Listing 1. xmlpage. aspx

Program code <% @ page Language = "VB" contenttype = "text/XML" %>
<? XML-stylesheet type = "text/XSL" href = "Copy. XSL"?>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en"
Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> my page </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: textbox id = "txtfirstname" runat = "server"/>
</Div>
</Form>
</Body>
</Html>

The page command will render the page as text/XML. The second line in the list references An XSLT style table named copy. XSL, which performs ID conversion on the current document. In other words, apart from copying all elements in the original XML document to the new HTML document, it does nothing at all. The source code of copy. XSL is included in Listing 2.

Listing 2. Copy. XSL

Program code <stylesheet version = "1.0"
Xmlns = "http://www.w3.org/1999/XSL/Transform">
<Template match = "/">
<Copy-of select = "."/>
</Template>
</Stylesheet>

This solution is effective, but it does not seem very exciting. When analyzing XML documents, additional verification steps are indeed obtained. However, if you generate an ASP. NET page in Visual Studio. NET 2005 or visual web developer, the development environment will perform the same verification in the "Source" view. Finally, Internet Explorer receives the same document as the text/html document sent to it.

The third option-content negotiation, best combination of W3C recommendation spirit and maximum browser compatibility (see http://www.w3.org/2003/01/xhtml-mimetype/content-negotiation ). When content negotiation is used, ASP. NET pages are provided to different browsers for different MIME types. If the browser claims that it supports XHTML, it will be provided with an XHTML-type page; otherwise, it will be provided with a page of text/html MIME type.

Global. asax in listing 3 contains the Code required to provide different MIME types pages to different browsers. If the file is added to a web project, the MIME type of each ASP. NET page is modified with each request. When a page is provided to Firefox or opera, the page is provided as application/XHTML + XML. Internet Explorer 6 receives text/html pages.

Listing 3. Global. asax

Program code <SCRIPT runat = "server">
Sub application_presendrequestheaders (byval s as object ,_
Byval e as eventargs)
If array. indexof (request. accepttypes ,_
"Application/XHTML + XML")>-1 then
Response. contenttype = "application/XHTML + XML"
End if
End sub
</SCRIPT>

From: http://www.flywe.net/article.asp? Id = 133

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.