URL rewriting of Asp.net static pages is very suitable

Source: Internet
Author: User

Asp.netOfURLRewrite

AboutURLThis article is just an idea. SuccessivelyMS"Urlrewriter "and"Application_beginrequest () "encoding method, and ISAPI settings in IIS.

The implementation method is also very simple.

Method 1: MS components

You do not need to go into details here. For details, refer:

Http://www.microsoft.com/china/msdn/library/webservices/asp.net/URLRewriting.mspx

Easy to use, just put the componentsUrlrewriter. dllCopy to ApplicationProgramOfBinDirectory, and thenWeb. configAdd the followingCode:

In<Configuration> </Configuration>Add:

<Configsections>

<SectionName= "Rewriterconfig"Type= "Urlrewriter. config. rewriterconfigserializersectionhandler, urlrewriter"/>

</Configsections>

<Rewriterconfig>

<Rules>

<Rewriterrule>

<Lookfor>~ /("D {4})/(" d {2})/Default ". aspx</Lookfor>

<Sendto>~ /Default. aspx? Id = $1</Sendto>

</Rewriterrule>

</Rules>

</Rewriterconfig>

Then<System. Web> </System. Web>Add:

<Httphandlers>

<AddVerb= "*"Path= "*. Aspx"

Type= "Urlrewriter. rewriterfactoryhandler, urlrewriter"/>

</Httphandlers>

Finally, enter http: // localhost/test/2004/12/news. aspx in the address bar.

The result is displayed.

The above<Lookfor>~ /("D {4})/(" d {2})/News ". aspx</Lookfor>This regular expression URL is the URL to be rewritten.<Sendto>~ /Default. aspx? Id = $1</Sendto> This sentence is the original URL address. $1 indicates the value of the First Regular Expression (2004 in the preceding example), and so on. The second is $2. 

Method 2:Application_beginrequest ()

Create an XML file in the application with the file name rewriter. config

<?XMLVersion= "1.0"Encoding= "UTF-8"?>

<Rewriterurls>

<Rule>

<Old>(. *)/News/("d {4})/Default". aspx</Old>

<New>Http://www.cnblogs.com/Default.aspx? Id = $2& Amp;Type = $3</New>

</Rule>

</Rewriterurls>

In the global. asax fileApplication_beginrequest (Object sender, eventargs E) is added to the Code:

Try

{

StringPath = server. mappath ("~ /Rewriter. config ");

Xpathdocument myxpathdocument =NewXpathdocument (PATH );

Xpathnavigator myxpathnavigator = myxpathdocument. createnavigator ();

Xpathnodeiterator myxpathnodeiterator = myxpathnavigator. Select ("// rule ");

System. Text. regularexpressions. RegEx oreg;

StringRewriteurl;

While(Myxpathnodeiterator. movenext ())

{

// Oreg = new RegEx (onode. selectsinglenode ("url/text ()"). value );

Xpathnavigator nav2 = myxpathnodeiterator. Current. Clone ();

StringOldstring = "", newstring = "";

Xpathnodeiterator it2 = nav2.select ("old ");

While(It2.movenext ())

{

Oldstring = it2.current. value;

Break;

}

It2 = nav2.select ("new ");

While(It2.movenext ())

{

Newstring = it2.current. value;

Break;

}

If(Oldstring! = "" & Newstring! = "")

{

Oreg =NewSystem. Text. regularexpressions. RegEx (oldstring );

If(Oreg. ismatch (request. url. tostring ()))

{

Rewriteurl = oreg. Replace (request. url. tostring (), newstring );

Httpcontext. Current. rewritepath (rewriteurl );

Break;

}

}

}

}

Catch

{

}

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.