Asp.net Tutorial: ReWrite to implement second-level domain name

Source: Internet
Author: User

  Jzquan. Com)Recently, I had a friend who wanted to rewrite the url. I helped him with this. I looked back at the article I wrote and thought it was easy to understand. But now I think it is not easy to write. Today, Baidu's urlrewriter finds that my article ranks second. To make it easier for more friends, I will write something to add.

In general, Baidu search is not familiar with this, but is in urgent need of use. I think, more languages than a single 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. For example, http: // song2. Cn/viewnews/2009/3/2. Rewrite the html url. Rewrite it to http: // song2. Cn/viewnews. Aspx? Id = 2 & year = 2009 & month = 3

2. Download the Microsoft URLRewriter. Dll, use this to rewrite the url.

Let's take a look at this url, http: // song2. Cn/viewnews/2009/3/2. Html (or 3, of course. Html, 6. Html, 7. Html ). To rewrite the url, 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 this end, create a new class library named URL and reference URLRewriter in this class library. Dll. Create a class named myrewritter. Cs, the Code is as follows:

Code highlighting produced by Actipro CodeHighlighter (freeware) http: // www. CodeHighlighter. Com/--> 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 add our own logic to the Rewrite method. Now, as long as the current url request is viewnews/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. The relationship between the page and the page may need to be queried through the database.

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

Reference content is as follows:
<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 an httpModules. The role of this is, if there is a url request, asp. Net will first pass the request to the class you specified. Add the line. The class before which the request is to be received is followed by the dll name of the class. After this sentence is added, everything will soon end. Because what we want to deal with is. Html page, so you need to configure iis. The default value is asp. Net Processing Engine. Html. What we need to do is to process asp. The program of ent to process html pages. Open iis. Find your website and right-click to open properties. Find the Home Directory, 991--995--055, 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. Then click Add. In the displayed dialog box, Executable paste the copied path here, fill in html for extension, and save it after 80 MM. In this way, we will be right. The html page request is handed over to asp. . Net.

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

In my example, the url of a web site is configured with 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 studying flex recently, and I am still unable to bear with some new things. When spring is approaching, everyone is very upset. So, let's calm down and take a while.

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.