Asp.net rewrite urlrewriter with URL to implement any second-level domain name (continued)

Source: Internet
Author: User

For more advanced information, see:
Http://www.cnblogs.com/notus/archive/2007/03/13/673222.html
Recently, a friend has something to do with URL rewriting. I helped him with it and looked back at the one he wrote.ArticleI thought it was easy to understand what I wrote. but now I think it is not easy to write. today, Baidu's urlrewriter found that my article was ranked second. to make it easier for more friends, I will write something to add.
Generally, Baidu search's friends are not familiar with this but are in urgent need of it. I think there are more languages than one sentence.Code. So I wrote a small example for my friends today. the purpose of this small example is to rewrite any URL (but does not include a second-level domain name. If you need a second-level domain name, you can first understand the concept of URL rewriting ). the preparation process of this small project is as follows:

1. You need? Id = 2 & year = 2009 & month = 3
2. Download the Microsoft urlrewriter. dll and rewrite the URL.

Let's look at this URL, http://song2.cn/viewnews/2009/3/2.html (of course it can also be 3.html, 6.html, 7.html)To rewrite the URL request, first intercept the URL request, analyze the current URL, and finally jump to the corresponding page..So our first step is to intercept URL requests.To create a new class library named URL, reference urlrewriter. dll in this class library to create a class named myrewritter. cs. The Code is as follows:

 

Namespace URL
{
Public   Class Myrewritter: urlrewriter. basemodulerewriter
{
Protected   Override   Void Rewrite ( String Requestedpath, httpapplication APP)
{
If (Requestedpath. Contains ( " Viewnews/2009/3/2. html " ))
App. Context. rewritepath ( " /Viewnews. aspx? Id = 2 & year = 2009 & month = 3 " );
Else
App. Context. rewritepath ( " /Here. aspx " );
}
}
}

We can see that this class inherits urlrewriter. basemodulerewriter, and then adds our own logic to the rewrite method. Now, as long as the current URL request isViewnews/2009/3/2. html, We will rewrite the page to/viewnews. aspx? Id = 2 & year = 2009 & month = 3.
This is just a simple example. In fact, the URL judgment is generally completed using a regular expression, and the correspondence between pages may need to be completed by querying the database.

Next, reference this class library in the website project. Then modify web. config. below is my web. config

 

<? XML version = "1.0" ?>

< Configuration >
< Appsettings />
< Connectionstrings />
< System. Web >
< Httpmodules >
< Add Type = "Url. myrewritter" Name = "Url"   />
</ Httpmodules >
< Compilation Debug = "True"   />
< Authentication Mode = "Windows"   />
</ System. Web >
</ Configuration >

 

You can see that I have added httpmodules. This function is that if there is a URL request, Asp.net will first upload the request to the class you specified.The. Add line is followed by the class to receive the request, followed by the DLL name of the class.

After this sentence is added, everything will soon end..Because we are dealing with the web pages of. html, The iis.net processing engine cannot be configured. html. What we need to do is to use the one that handles ASP. Ent.ProgramTo process HTML pages.Open IIS. find your website and right-click to open properties. find the home directory and open the configuration dialog box. in the Application Extensions (sorry, I don't know what this is called in the Chinese system) list. aspx, click Edit ...), copy the content of the executable box. for example, my website is c: \ windows \ Microsoft. net \ framework \ v2.0.50727 \ aspnet_isapi.dll. click Add. In the displayed dialog box, executablepaste the copied token to this page, and fill in extensionhtml. Then, it is guaranteed. In this case, we handed over the request for the .html page to Asp.net.

After all, compile the project (generated in the Chinese environment) and open the URL http: // localhost/mytest/viewnews/2009/3/2. HTML to see if it will be rewritten to http: // localhost // viewnews. aspx? Id = 2 & year = 2009 & month = 3

Below are all the files in my example. You can download them and check them out.Reading a code is more useful than reading ten sentences, right?

 

 

Http://files.cnblogs.com/notus/rewrittertest.rar

In my example, the URL of a web site is configured as http: // localhost: 8011. You may need to change this configuration. you can right-click a website project to open properties. then modify it on the web page.

As a beginner, these things may be annoying. if you don't want to read a long article, the best way is to look at my code. I have been learning flex recently, and I am still unable to bear with some new things. when spring is approaching, everyone is excited. So let's calm down and take a while.

Happy Programming

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.