URL rewrite for asp.net

Source: Internet
Author: User
Tags config expression regular expression tostring
asp.net

ASP.net's Rul rewrite


The article is just an idea for rewriting URLs. A succession of MS Components "Urlrewriter" and "Application_BeginRequest ()" Encoding in Global.asax, as well as ISAPI settings in IIS.

Donna listed, the implementation method is also very simple.

Method One: Ms components

Here also do not need to explain, the relevant see:

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

The usage is simple, just copy the component URLRewriter.dll to the application's Bin directory, and then add the following code under Web.config:

Add in <configuration></configuration>:

<configSections>

<section name= "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 add in <system.web></system.web>:

<add verb= "*" Path= "*.aspx"

Type= "Urlrewriter.rewriterfactoryhandler, Urlrewriter"/>

Finally, type on the address bar: http://localhost/Test/2004/12/News.aspx

The effect came out.

The above <lookfor>~/(\d{4})/(\d{2})/news\.aspx</lookfor> This regular expression URL, which is the overridden URL, and <sendto>~/ Default.aspx?id=$1</sendto> This sentence is the original URL address. Where is the first regular expression value (the example above is: 2004), and so on, the second is $

Method Two: Application_BeginRequest ()

Create a new XML file in your application that contains the file name: Rewriter.config

<?xml version= "1.0" encoding= "Utf-8"?>

<ReWriterUrls>

<rule>

<old> (. *)/news/(\d{4})/default\.aspx</old>

<new> /.. /default.aspx?id=$2&type=$3</new>

</rule>

</ReWriterUrls>

In the Global.asax file, add the code in the Application_BeginRequest (Object sender, EventArgs e):

Try

{

String Path=server.mappath ("~/rewriter.config");

Xpathdo***ent myxpathdo***ent = new Xpathdo***ent (path);

XPathNavigator myxpathnavigator = Myxpathdo***ent. CreateNavigator ();

XPathNodeIterator myxpathnodeiterator = Myxpathnavigator.select ("//rule");

System.Text.RegularExpressions.Regex Oreg;

String Rewriteurl;

while (Myxpathnodeiterator.movenext ())

{

Oreg=new Regex (Onode.selectsinglenode ("Url/text ()"). Value);

XPathNavigator nav2 = MyXPathNodeIterator.Current.Clone ();

String Oldstring= "", 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 = new System.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.