Global and URLReWrite usage in ASP. NET
This example describes the usage of Global and URLReWrite in ASP. NET. Share it with you for your reference. The details are as follows:
Global. asax:
Sometimes called an ASP. NET application file, it provides a method to respond to application-level or module-level events at a central location. You can use this file to implement application security and other tasks.
Important: application_Start; application_BeginRequest; application_Error;
① Application_Start: Application_Start is executed when the website is accessed for the first time since the server was started.
② Application_Error: An unhandled exception occurs in the program.
③ Session_End: only sessions in the process are called. Sessions outside the session_End process are not called.
④ Application_BeginRequest: when a request is sent, application_BeginRequest is called. when accessing a static page, application_BeginRequest is not processed. IIS directly sends the static page file to the browser. The Application_BeginRequest method is called even if a page does not exist.
URLReWrite:
Ugly link: http: // localhost/viewPerson. aspx? Id = 1
Ugly! Virgo cannot bear it.
Handsome link: http: // localhost/viewPerson-1.aspx
How can I integrate the link into a handsome one?
Using application_BeginRequest, regardless of the page accessed, all pages except static pages are switched to other program processing principles.
Match the ugly link with a regular expression that triggers global when the user accesses http: // localhost/viewPerson-1.aspx. asax calls the application_BeginRequest method. After the regular expression matches successfully, execute Context. rewritePath ("/ViewPerson. aspx? Id = "+ id); make it easy to integrate into the handsome link.
Use a regular expression:
1 2 3 4 5 6 7 8 9 |
Protected void Application_BeginRequest (object sender, EventArgs e) { Match match = Regex. Match (Context. Request. Path, @ "^/ViewPerson \-(\ d +) \. aspx $ "); If (match. Success) { String id = match. Groups [1]. Value; // get (\ d +) is the Value of id Context. RewritePath ("/ViewPerson. aspx? Id = "+ id ); } } |