. Net website development URLRewrite

Source: Internet
Author: User

1. URL Rewrite

URL Rewrite is the process of intercepting incoming Web requests and automatically redirecting requests to other URLs.

For example, if the browser sends a request to 51biancheng.com/101.aspx, the server will automatically redirect this request to the http://51biancheng.com/list.aspx? Id = 101.

URL Rewrite has the following advantages:

1. Shorten the URL and hide the actual path to improve security
2. easy for users to remember and type
3. Easy to be indexed by search engines

There are many ways to implement URL Rewrite, which should be a common one. The advantage of this method is that you do not need to change any IIS-related settings, and can also be used in Medium-Trust Security Level virtual hosts.

II. Implementation Details
1. Prepare the URLRewrite. NET class library and reference this class library on the website;

2. Add configSections to the first section of configuration:

<ConfigSections>
<Section name = "rewriter"
RequirePermission = "false"
Type = "Intelligencia. UrlRewriter. Configuration. RewriterConfigurationSectionHandler, Intelligencia. UrlRewriter"/>
</ConfigSections> 3. Add the processing of httpModule to system. web:

<HttpModules>
<Add name = "UrlRewriter" type = "Intelligencia. UrlRewriter. RewriterHttpModule, Intelligencia. UrlRewriter"/>
</HttpModules> 4. Add rewrite settings in configuration:

<Rewriter>
<Rewrite url = "~ /Articles/(d +) "to = "~ /Articles/Detail. aspx? Id = $1 "/>
<Rewrite url = "~ /Categories/(d +) "to = "~ /Articles/List. aspx? Id = $1 "/>
<Rewrite url = "~ /Categories/(d +) _ (d +) "to = "~ /Articles/List. aspx? Id = $1 & amp; page = $2 "/>
</Rewriter> note that when multiple parameters are used, HTML Escape characters must be used instead of the & symbol, or cdata can be used.

Iii. IIS configuration
In IIS 7, URLRewriter. NET can be used without configuration, but in IIS 6, some configuration is required. For details, see the following link.

Http://urlrewriter.net/index.php/support/installation/windows-server-2003

Iv. ASP. NET Postback settings
After UrlRewriter is used, ASP. the Postback mechanism in. NET causes the original link of the page to be used in the browser after the page is sent back, which is easy to confuse users and is not conducive to search by the search engine. Therefore, it must be processed.

After ASP. NET 2.0, we can use the extension framework of the control adapter to customize and override the action attribute of <form> without making any changes to the page code.

1. In App_Code, add the FormRewriter. cs file:

Using System. Web;
Using System. Web. UI;
 

Public class FormRewriterControlAdapter: System. Web. UI. Adapters. ControlAdapter
{
Protected override void Render (HtmlTextWriter writer)
{
Base. Render (new RewriteFormHtmlTextWriter (writer ));
}
}

Public class RewriteFormHtmlTextWriter: HtmlTextWriter
{
Public RewriteFormHtmlTextWriter (HtmlTextWriter writer)
: Base (writer)
{
InnerWriter = writer. InnerWriter;
}

Public RewriteFormHtmlTextWriter (System. IO. TextWriter writer)
: Base (writer)
{
InnerWriter = writer;
}

Public override void WriteAttribute (string name, string value, bool fEncode)
{
If (name = "action "))
{
HttpContext Context = HttpContext. Current;
If (Context. Items ["ActionAlreadyWritten"] = null)
{
Value = Context. Request. RawUrl;
Context. Items ["ActionAlreadyWritten"] = true;
}
}
Base. WriteAttribute (name, value, fEncode );
}
}
2. In App_Browsers, add the Form. browser file:

<Browsers>
<Browser refID = "Default">
<ControlAdapters>
<Adapter controlType = "System. Web. UI. HtmlControls. HtmlForm" adapterType = "FormRewriterControlAdapter"/>
</ControlAdapters>
</Browser>
</Browsers> after processing, you can ensure that the page still uses the original URL as the link after Postback occurs.

In addition, for some very fixed mappings, I personally think that using urlmapping is also a good choice, such as in the website about us, you put aboutus. aspx? You need to add <urlMappings enabled = "true"> <add url = "~ /Ourhistory. aspx "mappedUrl = "~ /AboutUs. aspx? Id = 2 "/> </urlMappings> This is a built-in. net support that is easy to use and efficient.

 

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.