We often see a lot of website visitsArticleIs ***. HTML or ***. shtml (such as the Log Access Effect of this blog) does not exist on the server at the time. Why does this effect occur because the URL is rewritten on the Web server, by Rewriting the accessed URL into an internal access page according to a specific format, the advantage of the URL is that it is easy for users to understand, while the search engine can also better earn your website, of course, there are many other benefits. I will not introduce them here.
This article describes how to use httphandler in ASP. NET to rewrite a URL. For how it works, see here.ProgramAny URL can be processed, because I used a URL in the program for excessive consideration. Only a number is used to access the file name, and a new page is executed internally and data is output,CodeAs follows:
Public Void Processrequest (httpcontext context)
{
Try
{
// Declarative request
Httprequest request = Context. request;
// Obtain the absolute path of the URL
String URL = Request. url. absolutepath;
// Start character interval of the accessed Web file
Int Regstart = URL. lastindexof ( " / " ) + 1 ;
// Declare whether all the Web file names are numbers
RegEx Reg = New RegEx ( @" \ D + " );
// Match with regular expressions
If (Reg. ismatch (URL, regstart ))
{< br> // If the Web file name is a number, query related articles, run the specified page
context. server. execute ( " ~ /Permalink. aspx? Id = " + Reg. match (URL, regstart ). value);
}
}
Catch
{
Context. response. Redirect (context. Request. url. tostring ());
}
}
Of course, the first thing you need to do is to first create a class and inherit from ihttphandler. Then, compile the code into and compile it. To use this function in a web project, you must add the following statement in Web. config:
<Httphandlers>
<Add verb = "*" Path = "*. shtml" type = "httphandle. urlrewrite"/>
</Httphandlers>
In addition, you must configure the WEB Project in IIS. In the WEB Project Properties, on the Home Directory tab, change the execution permission to "script and executable file", and then enable the configuration, add the extension of the file format to be rewritten to the application extension.