The examples in this article describe global and urlrewrite usage in asp.net. Share to everyone for your reference. Specifically as follows:
Global.asax:
Sometimes called asp.net application files, provides a way to respond to application-level or module-level events in a central location. You can use this file to implement security for your application and some other tasks.
Key understanding: Application_Start; Application_BeginRequest; Application_Error;
①application_start: Since the server started up, the site was first visited Application_Start execution
②application_error: Unhandled exception occurred in program
③session_end: Only sessions within the process will be invoked, and the session outside the Session_End process will not
④application_beginrequest: When a request comes in, it calls Application_BeginRequest, and when you access the static page, Application_BeginRequest doesn't handle it. IIS gives the static paging file directly to the browser. The Application_BeginRequest method is invoked even if a nonexistent page is accessed.
Urlrewrite:
Ugly Link: http://localhost/viewPerson.aspx?id=1
It's ugly! Virgo cannot endure.
Handsome Link: http://localhost/viewPerson-1.aspx
How do you make a link like that?
Using Application_BeginRequest, no matter what page to access, in addition to static pages, are turned to other programs to deal with the principle.
The "ugly link" is matched with a regular expression, and when the user accesses the http://localhost/viewPerson-1.aspx, the Global.asax call Application_BeginRequest method is triggered. After the regular expression match succeeds, executes Context.rewritepath ("/viewperson.aspx?id=" + ID); Buttoned up, the whole into "handsome link", it is so simple.
Use regular Expressions:
| 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 the ID context.rewritepath ("/viewperson.aspx?id=" + ID); } } |