Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The realization method of URL Rewrite in ASP.NET

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about the implementation of URL Rewrite in ASP.NET. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

I thought this topic had been overtalked before. URL Rewrite has long been accepted by the majority of developers, and there are endless online components and articles about URL Rewrite, but it always makes me feel full of meaning, so I can't help but write this series of articles in the end. These articles will not talk about the value and significance of URL Rewrite, but will only talk about purely technical content. In this article, there will be no detailed analysis, but combined with my experience to explain this topic from the perspective of application. What you already know, what you don't know, what you've talked about elsewhere, or what you haven't said yet, I hope the "re-mention" of this series of articles will not make you feel boring and will let you know all aspects of URL Rewrite in ASP.NET. If you can think of these articles when you encounter problems with URL Rewrite in the future, I guess I will laugh out loud in my dreams.

To fully understand the topics discussed later in the article, we must briefly understand the communication process between IIS and ASP.NET. I'm talking about the IIS 6 server here. As for IIS 5 and IIS 7, the former can be said to be obsolete, while the latter's "classic mode" is exactly the same as IIS 6, while the new "pipeline mode" actually talks about the deep integration of some concepts in ASP.NET with IIS. I'm sure that if you understand IIS 6 and ASP.NET, you won't have any problems in IIS 7 integrated mode.

First, let's look at a simple schematic diagram that shows several main steps in the process of IIS from receiving the Request to returning the Response:

1.IIS received the request.

two。 The selector selects an ISAPI to process the request based on the characteristics of URL and the configuration in IIS-now it naturally chooses ASP.NET ISAPI

The 3.ASP.NET execution engine receives the request and initializes the data (such as building various objects)

4. Start triggering various Pipeline events, naturally starting with BeginRequest

5. After several Pipeline events, ASP.NET selects an appropriate Handler or HandlerFactory for the current request according to the configuration (except in special cases, for example, the result has been output directly in the previous event and the request has been terminated)

6. After Handler processing, there are several Pipeline events that end with EndRequest

7. Output Response.

If you want to do URL Rewrite in an ASP.NET application, you usually call the RewritePath method of HttpContext in the BeginRequest event to "relocate" the request to a target URL. For example, we can override the Application_BeginRequest method in Global.asax to achieve this:

The reason for Rewrite in BeginRequest is that this event is the first of all Pipeline events to be triggered. After repositioning at this point, some of the properties in the current HttpContext have changed accordingly (for example, HttpContext.Request.Path). In this way, the handler logic of the next Pipeline event will be affected. For example, when you need to determine permissions based on a directory, the path after "location" is used instead of the request received by ASP.NET. Naturally, the most "significant" change is the choice of Handler. For example, in the above example, we relocate the request to the "CustomerList.aspx" file so that the ASP.NET engine selects the System.Web.UI.PageHandlerFactory class corresponding to * .aspx to process the request.

Public class Global: System.Web.HttpApplication {protected void Application_BeginRequest (object sender, EventArgs e) {HttpContext context = HttpContext.Current; if (context.Request.Path.Equals ("/ Customers", StringComparison.InvariantCultureIgnoreCase)) {context.RewritePath ("~ / CustomerList.aspx");}

In addition, there are two concepts that need to be distinguished, that is, "ASP.NET Pipeline" and "Web Forms". Both are important models in ASP.NET, but there is a big difference:

◆ ASP.NET Pipeline: as a request received by each ASP.NET application, it is processed through this "pipeline". This is an ASP.NET-level model.

◆ Web Forms: during the execution of the 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 chooses the System.Web.UI.PageHandlerFactory class, in which the WebForms model is finally formed.

In fact, the word "formation" in the above sentence may not be very accurate. Because Web Forms should probably be an execution engine and model that can be used independently, and System.Web.UI.PageHandlerFactory only takes advantage of this model. When we write ASP.NET applications, we can use this model elsewhere according to our needs.

After reading the above, do you have any further understanding of the implementation of URL Rewrite in ASP.NET? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report