asp.net use URL rewrite urlrewriter to implement any two-level domain name New _ Practical Skills

Source: Internet
Author: User
Baidu is generally used to search friends are not aware of this but also need to use, I think that no amount of language is more than a code. So I sent up a little example of a friend I wrote today. The purpose of this small example is to implement an override of any URL (excluding the level two domain name). If you need a level two domain name, you can also understand the concept of URL rewriting first. The production process for this small project is as follows
1. A URL such as http://jb51.net/viewnews/2009/3/2.html needs to be overridden. Rewrite it as http://jb51.net/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://jb51.net/viewnews/2009/3/2.html (or 3.html,6.html,7.html, of course). To rewrite, first intercept the URL request, and then parse the URL, Finally jump to the appropriate page. So our first step is to intercept the URL request. To create a new class library called URL, reference URLRewriter.dll in the class library. Create a new class Myrewritter.cs with the following code
Copy Code code 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");
}
}
}

You can see that the class inherits Urlrewriter.basemodulerewriter and then adds 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, for the decision of the URL, is usually done with regular expressions. And the correspondence between pages and pages may need to be done through a query database.

Next, refer to this class library in the Web site project. And then revise the web.config, and here's my web.config.
Copy Code code as follows:

<?xml version= "1.0"?>
<configuration>
<appSettings/>
<connectionStrings/>
<system.web>
<add type= "Url.myrewritter" name= "URL"/>
<compilation debug= "true"/>
<authentication mode= "Windows"/>
</system.web>
</configuration>

You can see I've added a new httpmodules. The effect of this is that if there is a URL request, ASP.net will first send the request to the class you specified. Add that line, the front is the class to receive the request, followed by the name of the DLL in which this class resides.
After adding this sentence, it's almost over. Because we're dealing with an. html page, So you need to configure IIS. Because the default asp.net processing engine does not manage. html. What we need to do is process the HTML page with the program that handles the asp.ent. Turn on IIS. Locate your site, and then right-click to open the property. Find Home Directory Open the Configuration (Configuration) dialog box. In application extensions (sorry I don't know what this is in the Chinese system) list, find. aspx, click Edit (Edit ...) to copy the contents of the executable box. For example, my name is: C:\WINDOWS\ Microsoft.net\framework\v2.0.50727\aspnet_isapi.dll. Then click Add, in the Open dialog box, executable to paste the path just copied here, extension fill in the HTML, and then confirm the save. So we give the request to the. html page to asp.net.
All completed, compiled under the project (Chinese environment called generation), open the URL http://localhost/mytest/viewnews/2009/3/2.html, see will not rewrite to http://localhost// Viewnews.aspx?id=2&year=2009&month=3
Here are my examples of all the files that you can download and see. It's better to read a code than 10 words, right?
Http://xiazai.jb51.net/200911/yuanma/rewrittertest.rar
Note that in my example, the URL of the Web site is configured to http://localhost:8011, and you may need to change this configuration. The method is to right-click on the Web site project and open the property. Then modify it in the Web page.
As a beginner, you may feel annoyed about these things. If you don't want to see a long speech, the best way to do that is to look at my code. I've been learning flex recently and I'm in a state of unbearable for some new things. Spring is coming, everyone is more irritable, so, calm down, slowly.
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.