Simple URL rewriting in ASP. NET

Source: Internet
Author: User

Recently I used ASP. NET to write a small website. Program To rewrite the URL. Implementing URL rewriting in ASP. NET is an old topic, which is also available in Microsoft's msdn. Article This topic is specifically discussed:
Bytes Code . I am a programmer who likes to be lazy and is not familiar with ASP. NET programming and has a small website. So I came up with a simple method to rewrite the URL.
First, let's take a look at the purpose of URL rewriting. The ultimate goal of URL rewriting is to make it hard to understand "&? "The signed URL address with parameters is changed to a natural simple URL address that is easy to understand. It can be considered that all means to achieve this result are URL rewriting.
For example, a page address "http: // server/Mers MERs. aspx? Region = region name ", which displays the customer list of the specified region. If you enter the" http: // server/Mers MERs/region name "style, the URL is overwritten, it is converted to "http: // server/Mers MERs. aspx? Region = region name ". For example, if the client is" http: // server/Mers MERs/northeast. aspx ", the server will eventually execute" MERs. aspx? Region = Northeast ", URL rewriting makes the URL of the Web application user-friendly and improves the user availability of the program.
But the symbol "/" is a special character, which can be interpreted as the operating system directory separator. This may need to be judged during URL rewriting. I don't want to judge it, so there is no "/" symbol, other rewriting methods are used, such as entering "http: // server/Northeast customers. aspx is rewritten to "MERs. aspx? Region = northeast China. This method is simple and the program can be easily judged. You do not need to program the HTTP module and modify the configuration file. After customers. aspx is implemented, you only need to add the following code to global. asax.

Protected   Void Application_beginrequest (Object sender, eventargs E)
{
String Name =   Base . Request. filepath;
If (Name ! =   Null )
{
Int Index = Name. lastindexof ( ' / ' );
If (Index > =   0 )
Name = Name. substring (Index +   1 );
Index = Name. lastindexof ( ' \\ ' );
If (Index > =   0 )
Name = Name. substring (Index +   1 );
Index = Name. lastindexof ( ' . ' );
If (Index > =   0 )
Name = Name. substring ( 0 , Index );
Name = Name. Trim ();
// Here name is the simple name of the request address.
If (Name. endswith ( " Customer " ))
{
Name = Name. substring ( 0 , Name. Length -   2 );
Base . Context. rewritepath ( " Customers. aspx? Region = "   + System. Web. httputility. urlencode (name ));
// If response. Redirect is used, the address entered in the client's IE address bar will change.
// Base. Context. rewritepath ("customers. aspx? Region = "+ name, true );
}
}
}

This method only uses dozens of lines of code to implement simple URL rewriting, without the need to implement various interfaces, without modifying the configuration file. It is really convenient, and my laziness desire is satisfied.

However, this is a simple method of URL rewriting, which can only deal with simple problems. If you need to implement complicated URL rewriting, we recommend that you look at the URL rewriting solution proposed by Microsoft.

Download an ASP. NET demo program that implements simple URL rewriting (/files/xdesigner/urlrewritedemo.rar ). Note that for ASP.. net. If the URL parameter contains Chinese characters, the problem may occur. You need to modify the web. the requestencoding attribute value of the globalization element in config is "gb2312", but this problem does not exist if URL rewriting is used.

Yuan Yongfu (http://www.xdesigner.cn)

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.