Asp.net rewrite URLReWriter with url to implement any new second-level domain name

Source: Internet
Author: User

In general, Baidu search is not familiar with this, but is in urgent need of use. I think, more languages than a 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://jb51.net/viewnews/2009/3/2.html (of course it can also be 3.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 create a new class library named URL, reference URLRewriter in this class library. dll. create a class named myrewritter. cs, the Code is as follows: Copy codeThe 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 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, 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. configCopy codeThe Code is as follows: <? 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 an httpModules. this function is that if there is a url request, asp.net will first upload the request to the class you specified. add the row. The class before which the request is to be received is followed by the dll name of the class.
Invalid. 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 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 sentence is more useful than reading ten sentences, right?
Http://xiazai.jb51.net/200911/yuanma/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.