The requirement of the previous article is much simpler.
[STEP]
① Create a test directory in the project, and then create a page under the test directory: ax. aspx
② Add the application_beginrequest () method to global. asax. CS.
Protected Void Application_beginrequest (Object sender, eventargs E)
{
String Originalurl, newurl;
Originalurl = Request. url. pathandquery;
// Get the Page name, excluding the extension
Newurl = Originalurl. substring (originalurl. lastindexof ( ' / ' ), Originalurl. lastindexof ( ' . ' ) - Originalurl. lastindexof ( ' / ' ) + 1 );
Newurl = " ~ /Test " + Newurl + " Aspx " ;
/**/ /*This sentence is the essence
【~ /] Indicates the root directory of the website.
It's really strange. It used to indicate that the root directory does not use 【~] You can,
However, if the following statement is executed, an error is returned:
The virtual path "/test/ax. aspx" is mapped to another application, which is not allowed.*/
Httpcontext. Current. rewritepath (newurl );
}
③Register the. Ax suffix on IISFor more information, see chapter 5.
④ Configure the website for this project (see my previous post for details). In this example, the website name on my host is www.rewritepath.com.
⑤ Test, enter the following URL
Http://www.rewritepath.com/AX.ax
Http://www.rewritepath.com/AX.aspx
Http://www.rewritepath.com/sadfsa/sdf/AX.ax
Http://www.rewritepath.com/sad/fsa/sdf/AX.aspx
Actual path
: Http://www.rewritepath.com/Test/AX.aspx
Test passed, OK, very good!
[Expansion]
I. You can use regular expressions to rewrite URLs with different rules.
II. This method can be used as an overload method to implement extension with Parameters
C # |
Public
Rewritepath(
Filepath,
Pathinfo,
Querystring) |
Parameters
- FilepathInternal rewrite path.
- PathinfoThe additional path information of the resource.
- QuerystringQuery string.
I originally wanted to use this method to redirect between pages, and the result failed., Error:
Data cannot be verified.
But I can see the page Jump section on msdn.CodeI don't know how it works.
[Summary]
This is a simple and practical fast food. the method itself enables page Jump and the URL in the address bar remains unchanged. The previous article uses ihttphandler, which is somewhat close to the underlying layer and gives us a deep understanding of ASP. net is advantageous, but it can also implement more powerful functions.
[Appendix] This article references the URL rewriting Technology in ASP. NET 2.0 of Henry Liu, which is actually a simple version of Henry Liu.
URL: http://www.cnblogs.com/lgp/archive/2006/10/16/530426.html