Analysis of URL Rewrite in ASP. NET

Source: Internet
Author: User

I think this topic has been abused. URL Rewrite has already been accepted by many developers. The components and articles about URL Rewrite on the Internet are also endless, but it always makes me feel overwhelmed, so I can't help writing this series of articles. These articles will not talk about the value and significance of URL Rewrite, but will only talk about pure technical content. The article does not provide detailed analysis, but uses my experience to explain this topic from an application perspective. What you already know, what you do not know, what you have already talked about elsewhere, or what you have not said, I hope that the "old things" in this series of articles will not make you feel dull, it also helps you understand ASP. all aspects of URL Rewrite in. NET. If you encounter a problem with URL Rewrite in the future, you can think of these articles, and I may laugh in my dreams.

To fully understand the topics mentioned later in this article, we must briefly understand the communication process between IIS and ASP. NET. Here I will explain the IIS 6 server. As for IIS 5 and IIS 7, the former can be said to have been eliminated, while the latter's "Classic Mode" is similar to IIS 6, while the new "pipeline mode" is actually about ASP. some concepts in NET are deeply integrated with IIS. I believe that if you understand IIS 6 and ASP. NET, there will be no problems in IIS 7 integration mode.

First, let's take a look at a simple one, showing the main steps in IIS from receiving a Request to returning a Response:


1. IIS receives the request;

2. Select an ISAPI to process the request based on the URL characteristics and IIS configuration. Now ASP. net isapi is selected;

3. the ASP. NET execution engine receives the request, so the initialization data is like building various objects );

4. Start to trigger various Pipeline events. Naturally, start with BeginRequest;

5. after multiple Pipeline events, ASP.. NET selects a suitable Handler or HandlerFactory for Processing Based on the configuration for the current request. Of course, except in special cases, for example, the result has been directly output in the previous event and the request has ended );

6. After Handler processing, several Pipeline events are performed to end with an EndRequest;

7. Output Response.

In an ASP. NET application, if you want to perform URL Rewrite, you generally call the RewritePath method of HttpContext in the BeginRequest event to "locate" the request to a target URL. For example, we can rewrite the Application_BeginRequest method in Global. asax to achieve this.

The reason for Rewrite in BeginRequest is that this event was first triggered among all Pipeline events. After "locating" again, some attributes in the current HttpContext also change accordingly, for example, HttpContext. Request. Path ). In this way, the processing logic of the next Pipeline event will be affected. For example, when you need to determine permissions based on the directory, the path after "positioning" is used instead of the request received by ASP. NET. Naturally, the most "significant" Change is the Handler's choice. For example, in the previous example, we relocated the request to "mermerlist. ASP. NET engine will select *. the System. web. UI. the PageHandlerFactory class processes the request.

 
 
  1. publicclassGlobal:System.Web.HttpApplication  
  2. {  
  3. protectedvoidApplication_BeginRequest(objectsender,EventArgse)  
  4. {  
  5. HttpContextHttpContextcontext=HttpContext.Current;  
  6.  
  7. if(context.Request.Path.Equals("/Customers",  
  8. StringComparison.InvariantCultureIgnoreCase))  
  9. {  
  10. context.RewritePath("~/CustomerList.aspx");  
  11. }  
  12. }  

There are two concepts that need to be separated when inserting a sentence, namely "ASP. NET Pipeline" and "Web Forms ". Both are important models in ASP. NET, but the difference is very large:

ASP. NET Pipeline: requests received by each ASP. NET application are processed by this "Pipeline. This is an ASP. NET-level model.

Web Forms: In the execution process of ASP. NET Pipeline, one of the steps is to select an appropriate Handler or HandlerFactory) to process the request. If it is An aspx page, ASP. NET selects the System. Web. UI. PageHandlerFactory class, in which the WebForms model is formed.

In fact, the word "formation" in the above sentence may be inaccurate. Because Web Forms may be an execution engine and model that can be used independently, and System. Web. UI. PageHandlerFactory only utilizes this model. When compiling an ASP. NET application, we can use this model elsewhere as needed.

  1. Introduction to ASP. net mvc Framework
  2. Introduction to MvcAjaxPanel in ASP. NET MVC
  3. ASP. net mvc Framework to save UpdatePanel
  4. Use ASP. net mvc source code to find a solution
  5. ActionInvoker of ASP. net mvc Framework

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.