Asp. NET implement pseudo static method: use IHttpModule to implement

Source: Internet
Author: User
Tags config httpcontext sql injection

In ASP.net, there are many ways to implement pseudo static, we mainly introduce the implementation of this interface by IHttpModule to solve the problem today.

For the entire application, the IHttpModule interface is needed if we need to process the requested address when the request occurs. Commonly used to implement pseudo static technology. is to turn a get-access query string into a separate file. However, the value in the query string is actually accessed in the program. Such as:

Http://www.cnsaiko.com/news.aspx?id=1

Change to

Http://www. Cnsaiko.com/news_1.aspx

The advantage of this is to help SEO and prevent SQL injection and so on. Of course, file extensions can also vary with server support.

The IHttpModule interface has two methods for our implementation:

Dispose () Disposes of the resources (except memory) that are used by modules that implement IHttpModule.

Init () initializes the module and prepares it for processing requests.

The first step: if you want to use IHttpModule in your application, you need to configure the Web.config file.

In the Web.config file, we need to cast the httpmodules element, which is in the system.web element. As follows:

<add name= "HttpModule" type= "Webapplication1.httpmodule"/>

Note: The type must be the body path (including the namespace) of the types that implement the IHttpModule interface.

Step two: Realize IHttpModule

Creates a new HttpModule class that is the same as the type namespace path in the configuration file.

Use this class to implement the IHttpModule interface.

The code we started will be written in the Init method, where the HttpApplication object context includes all the methods, properties, and events of the process object. So in this object, we can start the processing of a request event.

You can write in the Init method:

Context. BeginRequest + = new EventHandler (context_beginrequest);

The above code registers a delegate method for the request start event.

When you register a delegate method, you can press the TAB key two consecutive times after you write + +, and automatically generate the Context_beginrequest method.

Step three: About Context_beginrequest () method

In the Context_beginrequest method, we can redirect the request according to the request. This turns the virtual pseudo static path into the Get-way query string that our program needs.

To change the request, you must get the current request, and in ASP.net, use the HttpContext type to encapsulate all the requested objects. So, we can get the HttpContext object from the HttpApplication object just now. Such as:

HttpApplication application = sender as HttpApplication;

HttpContext context = Application. context;

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.