URL rewriting Technology in ASP. NET 2.0 [conversion]
ImplementationURLHow many rewrite methods?
LExploitationApplication_beginrequestAndHttpcontextClassRewriteMethod RewritingURLThis method is easy to understand and easy to use.
LDevelopmentASP. net http ModuleTo achieve the same purpose
LDevelopmentISAPIFilter to capture the request to complete rewriting
Here,We will describe the first method.URLRewrite implementation
Application_beginrequestEvent
It isHTTPPipeline handles the first triggered event,Yes rewriteURLBest place
HttpcontextClass
This class containsHTTPSpecific information, includingHTTP request,Of courseResponseObject.
This class containsCurrentStatic attribute, which contains information about the current application.RewritepathThe method is to overrideURL. In2.0There are four signatures:
Public VoidRewritepath (StringPath );
Public VoidRewritepath (StringPath,BoolRebaseclientpath );
Public VoidRewritepath (StringFilepath,StringPathinfo,StringQuerystring );
Public VoidRewritepath (StringFilepath,StringPathinfo,StringQuerystring,BoolSetclientfilepath );
Move in one step
1.CreateC # Web ApplicationEngineering
2.OpenWebAdd the following configuration filesCode
< Appsettings >
< Add Key = "Productssite" Value = "Products" > </ Add >
< Add Key = "Servicessite" Value = "Services" > </ Add >
< Add Key = "Supportsite" Value = "Support" > </ Add >
</ Appsettings >
We put the corresponding folder name here, which will be used in later code.
3.Add threeFolder, products, Support Services
4.Add a default. aspx page to each file.
5.Open global. asax and check the event handle.
Protected Void Application_beginrequest ( Object Sender, eventargs E)
6.Add the following code to the preceding event:
String Host, originalurl, newurl;
Host = Request. url. Host;
Originalurl = Request. url. pathandquery;
Switch (Host)
{
Case " Products.henryliu.com " :
Newurl = " ~ / " +
Configurationsettings. deleettings [ " Productssite " ]
+ Originalurl;
Break ;
Case " Services.henryliu.com " :
Newurl = " ~ / " +
Configurationsettings. deleettings [ " Servicessite " ]
+ Originalurl;
Break ;
Case " Support.henryliu.com " :
Newurl = " ~ / " +
Configurationsettings. deleettings [ " Supportsite " ]
+ Originalurl;
Break ;
Default :
Newurl = " ~ / " +
Configurationsettings. deleettings [ " Supportsite " ]
+ Originalurl;
Break ;
}
Httpcontext. Current. rewritepath (newurl );
Let's take a look at this Code:
First, we useRequest. url. HostThe host name obtained from the property, for example:Support.henryliu.com,The query string of the current path is also obtained.SwitchThe statement is used to determine the page request to be executed based on the current user's request. Finally, we callRewritepath ()Method to override the current request'sURL.
total section: in this Article , we can learn how to use application_beginrequest and httpcontext. to rewrite URLs . This is a different method to quickly implement the actual request page and the URL we see.