Asp. Global and Urlrewrite usage in net

Source: Internet
Author: User

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

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.