Asp. NET Filter Application Method Introduction

Source: Internet
Author: User
In the Java EE Web Development, there are filter filters, the filter can intercept the specified URL access, and execute the filter method, according to the actual application, modify the request code in the filter, judge the session information, can also do permission control, in short, this filter is very meaningful, It can also be described as an application of the responsibility chain design pattern in the Java EE.

Is it possible to define such a filter structure in ASP, and to do the corresponding logical operation in the filter? The answer is yes, this article will tell you if you write a filter and how to configure it to the IIS Web App.

Procedure one: How to write a filter

Writing a filter, in fact, is to write a filter class, that is, to write a HttpModule module, this filter should implement the IHttpModule base class, and rewrite the Init method, give you a practical example is as follows:

This is a PageFilter.cs

using system;using system.web;using system.web.sessionstate;using System.collections.generic;using system.collections;using system.text;using system.io;public class PageFilter:        ihttpmodule{public String ModuleName {get {return ' Pagefilter ';}             }//Registered in the Init method HttpApplication//Register event public void Init (HttpApplication application) by means of a delegate { Application.                    AcquireRequestState + = new EventHandler (application_acquirerequeststate); } private void Application_acquirerequeststate (Object source, EventArgs e) {HttpApplication applicatio            n = (HttpApplication) source; HttpContext context = Application.            Context; HttpSessionState session = Context.            Session; HttpRequest request = context.            Request; HttpResponse response = context.            Response; String ContextPath = Request.        Applicationpath; }}

It is necessary to note that "filters" can also be called "interceptors", that is, the process of intercepting the entire HTTP request/response, because the entire request/response process can be divided into a number of stages, then this involves a problem, that is, your filter to intercept the specific stage, the above init function, You can define the specific stage you want to intercept, for example, the above interception is the stage of generating the request session, Acquirerequeststat is the representative of this state, and the corresponding processing function after interception is application_acquirerequeststate, So the following defines a application_acquirerequeststate method in which a series of objects, such as application, context, session, request, response, can be obtained by forcing type conversion. , on the basis of acquiring these objects, you can write the core business logic, for example, to determine whether the current URL access is legitimate, to check whether the current access is logged after the user's access and so on.


In addition, since there are many stages in the whole process of interception, how can we intercept other stages? This should be very simple, similar to the one above in Init as in the following logical definition:

Application. The standard name of the Stage 1 + = new EventHandler (this stage corresponds to the processing method name 1);
Application. The standard name of the Stage 2 + = new EventHandler (this stage corresponds to the processing method name 2);
。。。

The standard name of the stage is that these stages have standard names and are the standard properties of the Application object, such as the above AcquireRequestState, as well as BeginRequest, AuthenticateRequest, AuthorizeRequest, Resolverequestcache, AcquireRequestState, PreRequestHandlerExecute, PostRequestHandlerExecute, ReleaseRequestState, Updaterequestcache, endrequest many stages and so on, each of these stages has a specific meaning.

This stage corresponding to the processing method name, is actually your own definition corresponding to this stage processing method, above already has the sample, no longer explained.

There is also a need to pay special attention, there are so many stages can be intercepted, but in practical applications, we intercept often one or two stages, and note that some server objects can only be intercepted at a specific stage, such as the session object in the BeginRequest phase is not, In the AcquireRequestState and later stage, there are, therefore, according to the actual needs to intercept the specific stage, this is the most easy to meet the problem of novice.

Process two: How to configure Filtering

We have compiled a. cs file filter, so how to make this filter function, this needs to be configured, the default is definitely not intercept, you need to configure the filter to the application's Web. config file, the sample configuration is as follows:

<configuration><system.web> 

This is actually configured, and then publish the site generated DLL, etc., will be automatically blocked URL access, but to remember that, by default, all requests for the application will be blocked, if you point to intercept a specific request, such as to block only the ASPX file request, Then you can add the filter logic to the name of the file suffix, if not the ASPX directly let go

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.