[Note] Tips/tricks: rewrite URLs in ASP. NET

Source: Internet
Author: User

Original article address:
Tip: override URL http://blog.joycode.com/scottgu/archive/2007/03/01/94004.aspx in ASP. NET

Method 1: Use the Request. PathInfo parameter instead of the query string
Method 2: Use HttpModule to implement URL rewriting
Method 3: Use HttpModule in IIS7 to rewrite a URL without an extension
Method 4: Use ISAPIRewrite in IIS5 and IIS6 to rewrite URLs without extensions
Process ASP. NET PostBack in URL rewriting
Correctly process CSS and image references

Routine URL rewriting scenario
Http://www.store.com/products.aspx? Category = books

Http://www.store.com/products.aspx? Category = DVDs

Http://www.store.com/products.aspx? Category = CDs


Method 1: Use the Request. PathInfo parameter instead of the query string

Http://www.store.com/products.aspx/Books

Http://www.store.com/products.aspx/DVDs

Http://www.store.com/products.aspx/CDs



Method 2: Use HttpModule to implement URL rewriting

Http://www.store.com/products/Books.aspx

Http://www.store.com/products/DVDs.aspx

Http://www.store.com/products/CDs.aspx



Method 3: Use HttpModule in IIS7 to rewrite a URL without an extension

Http://www.store.com/products/Books

Http://www.store.com/products/DVDs

Http://www.store.com/products/CDs

IIS 7.0 makes it easy to handle such situations. You can now execute an HttpModule anywhere in the IIS request pipeline, which means you can use the URLRewriter module above to process and override URLs without extensions (or even URLs. asp ,. php, or. jsp extension URL ). The following shows how to configure IIS7:

<? Xml version = "1.0" encoding = "UTF-8"?>

<Configuration>

<ConfigSections>
<Section name = "rewriter"
RequirePermission = "false"
Type = "Intelligencia. UrlRewriter. Configuration. RewriterConfigurationSectionHandler, Intelligencia. UrlRewriter"/>
</ConfigSections>

<System. web>
<HttpModules>
<Add name = "UrlRewriter" type = "Intelligencia. UrlRewriter. RewriterHttpModule, Intelligencia. UrlRewriter"/>
</HttpModules>
</System. web>

<System. webServer>
<Modules runAllManagedModulesForAllRequests = "true">
<Add name = "UrlRewriter" type = "Intelligencia. UrlRewriter. RewriterHttpModule"/>
</Modules>
<Validation validateIntegratedModeConfiguration = "false"/>
</System. webServer>

<Rewriter>
<Rewrite url = "~ /Products/(. +) "to = "~ /Products. aspx? Category = $1 "/>
</Rewriter>

</Configuration>



Method 4: Use ISAPIRewrite in IIS5 and IIS6 to rewrite URLs without extensions



Process ASP. NET PostBack in URL rewriting

This problem occurs when URL rewriting is used. The <form> Control shows that the URL is not the original requested URL (for example,/products/books ), instead, the URL (for example,/products. aspx? Category = books ). This means that when you make a postback to the server, the URL is no longer the one you used to clean up. In ASP. NET 2.0, you can use it to override the action attribute of the <form> control. Specifically, you can use the new ASP. NET 2.0 control adapter extension architecture to customize the control output and overwrite the value of the action property with the value you provide. This is not required in your. make any encoding changes on the aspx page, and add one in your/app_browsers folder. browser file, register and use a control adaptation class to output new action attributes.



Correctly process CSS and image references

When using URL rewriting for the first time, many people sometimes encounter a difficult problem, that is, they find that their images and CSS style sheet references sometimes stop working. This is because they have relative references to these files on HTML pages. When you start to rewrite the URL in the application, you need to realize that browsers often request files at different logical hierarchy levels layers, rather than what is actually stored on the server.

For example, if the/products. aspx webpage. the logo.jpg file in the aspx webpage has a relative reference, but it is referenced by/products/books. when the browser displays a webpage, it will send a request to/products/logo.jpg instead of/logo.jpg. To reference this file correctly, make sure that you use the root directory to restrict (root qualify) CSS and image reference ("/style.css" instead of “style.css "). For ASP. NET controls, you can also use "~" Syntax reference a file from the root directory of your application (for example, <asp: image imageurl = "~ /Images/logo.jpg "runat =" server "/> ).

Attachment:/Files/kiant/2010.05/UrlRewrite_HttpModule1.FormRewriter.cs.zip file for the attached CS version

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.