Execute URL rewriting (rewritepath) in ASP. NET)

Source: Internet
Author: User

I read msdn and other people's documents and sorted out the URL rewriting in ASP. NET.

This is some information

Http://msdn.microsoft.com/zh-cn/library/ms972974.aspx#XSLTsection123121120120
Http://hi.baidu.com/yequanfu/blog/item/1513ce3325b54840ad4b5fbc.html
Http://hi.baidu.com/sunsonruby/blog/item/d933b7b45d5688778ad4b26e.html

First introduce an introduction

Let's take a moment to look at some URLs on the website. Have you found something similar to http://yoursite.com/info/dispEmployeeInfo.aspx? Empid = 459-099 & type = Summary URL? Alternatively, you may move a series of web pages from one directory or website to another directory or website. As a result, visitors who have used the old URL as a bookmarks will be disconnected. In this article, we will learn how to associate http://yoursite.com/info/dispEmployeeInfo.aspx? Empid = 459-099 & type = summary is replaced with a URL similar to a http://yoursite.com/people/sales/chuck.smith, using URL rewriting to abbreviated lengthy URLs as rich meaningful and easily remembered URLs. We will also learn how to rewrite a URL to create a smart 404 error.

URL rewriting is the process of intercepting incoming Web requests and automatically redirecting requests to other resources. During URL rewriting, the request is usually checked and redirected to another URL Based on the URL value. For example, when a website is reorganized and all the webpages in the/people/directory are moved to the/INFO/employees/directory, you may want to use URL rewriting to check whether the Web request points to a file in the/people/directory. If the request points to a file in the/people/directory, you may want to automatically redirect the request to the same file in the/INFO/employees/directory.

Using traditional ASP, the only way to rewrite a URL is to write an ISAPI filter or buy a third-party product that provides the URL rewriting function. However, Microsoft ASP. Net allows you to easily create your own URL rewriting software in many ways. This article discusses various technologies that can be used by ASP. NET developers to rewrite URLs, and then discusses some actual use cases of URL rewriting. Before going into the technical details of URL rewriting, let's take a look at some daily scenarios that can be rewritten using URL.

After the introduction in the above article, I have some knowledge about URL rewriting. The following is a small example.

First, introduce urlrewriter. DLL into the project.
Http://download.microsoft.com/download/0/4/6/0463611e-a3f9-490d-a08c-877a83b797cf/MSDNURLRewriting.msi
Urlrewriter and actionlessform are included here. If necessary, you can rewrite them and introduce. DLL into the project after compilation.

First, configure webconfig. In the Web. config file, specify whether to use the HTTP module or HTTP processing program to rewrite the URL. The HTTP module is used for processing.

<Configsections>
<Section name = "rewriterconfig" type = "urlrewriter. config. rewriterconfigserializersectionhandler, urlrewriter"/>
</Configsections>
<Httpmodules>
<Add type = "urlrewriter. modulerewriter, urlrewriter" name = "modulerewriter"/>
</Httpmodules>

If an HTTP handler is used, configure it in httphandlers.
<Httphandlers>
<Add verb = "*" Path = "*. aspx"
Type = "urlrewriter. rewriterfactoryhandler, urlrewriter"/>
</Httphandlers>

In addition to specifying whether to use the HTTP module or the HTTP processing program for rewriting. the Config File also contains a rewrite rule: the rewrite rule consists of two strings: The pattern to be searched in the requested URL; the pattern string to be replaced (if found ). In the Web. config file, this information is expressed in the following syntax:

<Rewriterconfig> <rules> <rewriterrule> <lookfor> the mode to be searched </lookfor> <sendto> string to replace the mode </sendto> </rewriterrule> <rewriterrule> <lookfor> the mode to be searched </lookfor> <sendto> string to replace the mode </sendto> </rewriterrule>... </rules> </rewriterconfig>

Each rewrite rule is composed of <Rewriterrule> Element expression. The pattern to be searched by <Lookfor> The element is specified, and the string to replace the pattern found will be in <Sentto> Input in the element. These rewrite rules are calculated from start to end. If the URL matches a rule, the URL is overwritten and the search for the rewrite rule is terminated.

In <Lookfor> When specifying a pattern in an element, note that regular expressions must be used for matching and string replacement. Since the mode is a regular expression, ensure that any reserved characters in the escape regular expression are retained. (Some regular expressions retain the following characters :.,? , ^, $, And others. You can escape these characters by adding a backslash (such as/.) to match the text periods .)

Add a node under configuration

<Rewriterconfig>
<Rules>
<Rewriterrule>
<Lookfor> ~ /(/D {4})/(/d {2})/(/d {2})/. aspx </lookfor>
<Sendto> ~ /Showblogcontent. aspx? Year = $1 & amp; month = $2 & amp; Day = $3 </sendto>
</Rewriterrule>
<Rewriterrule>
<Lookfor> ~ /(/D {4})/(/d {2})/default/. aspx </lookfor>
<Sendto> <! [CDATA [~ /Showblogcontent. aspx? Year = $1 & month = $2]> </sendto>
</Rewriterrule>
<Rewriterrule>
<Lookfor> ~ /(/D {4})/default/. aspx </lookfor>
<Sendto> ~ /Showblogcontent. aspx? Year = $1 </sendto>
</Rewriterrule>
<Rewriterrule>
<Lookfor> ~ /Modalpopupextender </lookfor>
<Sendto> ~ /Modalpopupextender. aspx </sendto>
</Rewriterrule>
</Rules>
</Rewriterconfig>

The preparation is complete. Check the effect.
When modalpopupextender is entered, it is directed to the modalpopupextender. ASPX page.

 

2. Use global. asax to rewrite the URL in application_beginrequest (Object sender, eventargs E ).

Prepare an XML file
<? XML version = "1.0" encoding = "UTF-8"?>
<Rewriterurls>
<Rule>
<Lookfor> modalpopupextender </lookfor>
<Sendto> modalpopupextender. aspx </sendto>
</Rule>
<Rule>
<Lookfor> (/d {4})/(/d {2})/(/d {2})/. aspx </lookfor>
<Sendto> showblogcontent. aspx? Year = $1 & amp; month = $2 & amp; Day = $3 </sendto>
</Rule>
<Rule>
<Lookfor> (/d {4})/(/d {2})/default/. aspx </lookfor>
<Sendto> <! [CDATA [showblogcontent. aspx? Year = $1 & month = $2]> </sendto>
</Rule>
<Rule>
<Lookfor> (/d {4})/default/. aspx </lookfor>
<Sendto> showblogcontent. aspx? Year = $1 </sendto>
</Rule>
</Rewriterurls>

Methods In beginrequest

Try
{

String Spath = server. mappath ("~ /Urlrewrite. config ");

System. xml. XPath. xpathdocument myxpathdocument = new system. xml. XPath. xpathdocument (Spath );
System. xml. XPath. xpathnavigator myxpathnavigator = myxpathdocument. createnavigator ();
System. xml. XPath. xpathnodeiterator myxpathnodeiterator = myxpathnavigator. Select ("// rule ");
System. Text. regularexpressions. RegEx oreg;

String rewriteurl;

While (myxpathnodeiterator. movenext ())
{

System. xml. XPath. xpathnavigator nav2 = myxpathnodeiterator. Current. Clone ();
String oldstring = "", newstring = "";
System. xml. XPath. xpathnodeiterator it2 = nav2.select ("lookfor ");
While (it2.movenext ())
{
Oldstring = it2.current. value;
Break;
}

It2 = nav2.select ("sendto ");
While (it2.movenext ())
{
Newstring = it2.current. value;
Break;
}
If (oldstring! = "" & Newstring! = "")
{
Oreg = new system. Text. regularexpressions. RegEx (oldstring, regexoptions. ignorecase );
If (oreg. ismatch (request. url. tostring ()))
{
Rewriteurl = RegEx. Replace (request. rawurl, oldstring, newstring, regexoptions. Compiled | regexoptions. ignorecase );
Httpcontext. Current. rewritepath (rewriteurl );
Break;
}
}
}
}
Catch {}

Finished. The execution result is the same...

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.