Global and URLReWrite usage in ASP. NET

Source: Internet
Author: User

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 );

}

}

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.