URL rewriting Technology in ASP. NET 2.0

Source: Internet
Author: User

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.

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.