Execute URL rewriting in ASP. NET (2)

Source: Internet
Author: User

• Events can be triggered when processing requests.

• Allows any number of HTTP modules to process events, which is similar to the ISAPI filter of IIS.

• Delegate the task of rendering the requested resource to HTTP for processingProgramThe handler is similar to the ISAPI extension of IIS.

Like IIS, ASP. NET engine triggers an event within the validity period of the request. It sends a signal to indicate that the processing process changes from one state to another. For example, when the ASP. NET engine responds to a request for the first time, the beginrequest event is triggered. Next, the authenticaterequest event is triggered, which appears when a user ID has been created. (In addition, there are a large number of other events: authorizerequest, resolverequestcache and endrequest. These events belong to the system. Web. httpapplication class. For more information, see the technical documentation at the following URL: httpapplication class overview .)

As discussed in the previous section, you can create an ISAPI filter to respond to events caused by IIS. Similarly, ASP. NET provides an HTTP module that responds to events triggered by the ASP. NET engine. You can configure ASP. NET web applications to have multiple HTTP modules. For each request processed by the ASP. NET engine, each configured HTTP module is initialized and the event handler can be bound to the event triggered during request processing. Note that many built-in HTTP modules are used for each request. One of the built-in HTTP modules is formsauthenticationmodule. This module first checks whether form authentication is used. If it is used, it checks whether the user has been authenticated. If not, the user is automatically redirected to the specified logon page.

As mentioned above, the request passed in through IIS will be finally sent to the ISAPI extension, and the task of ISAPI extension is to return the data of the specific request. For example, when requesting a traditional ASP Web page, IIS passes the request to the ASP. dll ISAPI extension. The task of this extension is to return the HTML tag of the requested ASP page. The ASP. NET engine uses similar methods. After the HTTP module is initialized, the next task of the ASP. net engine is to determine which HTTP handler should process the request.

All. the requests passed by the net engine will eventually arrive at the HTTP handler or HTTP handler Factory (the HTTP handler factory only returns the HTTP handler instance and then uses the instance to process the requests ). The final HTTP handler returns a response, that is, the requested resource. This response will be sent back to IIS, and IIS will return the response to the requesting user.

ASP. NET includes many built-in HTTP processing programs. For example, pagehandlerfactory is used to render an ASP. NET webpage. Webservicehandlerfactory is used to present the response SOAP envelope of ASP. NET web services. Tracehandler will present the request's HTML tag to trace. axd.

Figure 2 describes how to handle requests to ASP. NET Resources. First, IIS receives the request and schedules the request to aspnet_isapi.dll. Next, the ASP. net engine initializes the configured HTTP module. At last, the system calls the correct HTTP processing program, presents the requested resources, and returns the generated mark to IIS and the request client.

 

Figure 2. IIS and ASP. NET are processing requests

Create and register a custom HTTP module and HTTP handler
Creating a custom HTTP module and an HTTP handler is a relatively simple task, including creating a Managed class that implements the correct interface. The HTTP module must implement the system. Web. ihttpmodule interface, while the HTTP processing program and the HTTP processing program factory must implement the system. Web. ihttphandler interface and the system. Web. ihttphandlerfactory interface respectively. The details of creating HTTP processing programs and HTTP modules are beyond the scope of this article. For more background information, see the description of Mansoor Ahmed Siddiqui.ArticleHTTP handlers and HTTP modules in ASP. NET.

After creating a custom HTTP module or HTTP processing program, you must register it with the Web application. Register the HTTP module and HTTP handler for the entire web server only on the machine. in the config file, simply add the HTTP module or HTTP handler for a specific web application, including the web. add several lines of XML to the config file.

In particular, to add the HTTP module to a web application, add the following lines in the configuration/system. Web Section of Web. config:

<Httpmodules>
<Add type = "type" name = "name"/>
</Httpmodules>

The type value provides the HTTP Module assembly and class name, and the name value provides a friendly name. You can use this friendly name in the global. asax file to reference the HTTP module.

The

<Httphandlers>
<Add verb = "verb" Path = "path" type = "type"/>
</Httphandlers>

As mentioned above, for each incoming request, the ASP. net engine will determine which HTTP handler should be used to present the request. This decision is made based on the verb and path of the incoming request. The verb specifies the type (get or post) of the HTTP request, and the path specifies the location and file name of the requested file. Therefore, if you want the HTTP handler to process all requests (get or post) to files with the extension. Scott, you can add the following lines in the web. config file:

<Httphandlers>
<Add verb = "*" Path = "*. Scott" type = "type"/>
</Httphandlers>

Type is the type of the HTTP handler.

Note: When registering an HTTP handler, it is important to ensure that the extension used by the HTTP handler has been mapped from IIS to the ASP. NET engine. That is to say, in this. in the Scott example, if. if the Scott extension is not mapped from IIS to the aspnet_isapi.dll ISAPI extension, then the file Foo. scott's request will cause IIS to attempt to return the file Foo. scott's content. To enable the HTTP handler to process this request, the. Scott extension must be mapped to the ASP. NET engine. Then, the ASP. NET engine routes requests to the corresponding HTTP handler correctly.

For more information about registering the HTTP module and HTTP handler, see

Back to Top
Implement URL rewriting
You can use the ISAPI filter to rewrite the URL at the IIS web server level, or use the HTTP module or HTTP handler to rewrite the URL at the ASP. NET level. This article focuses on how to use ASP. NET to implement URL rewriting. Therefore, we will not go into the details of using ISAPI filter to implement URL rewriting. However, there are a large number of third-party ISAPI filters that can be used for URL rewriting. For example:

• ISAPI rewrite

• IIS rewrite

• Pagexchanger

• There are many other filters!

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.