In the Java EE Web development, there are filters filter filter, the filter can intercept the specified URL access, and execute the filter method, according to the actual application, in the filter to modify the requested code, to determine the session information, can also do permission control, in short this filter is very meaningful, It can be said that the responsibility chain design pattern in the Java EE an application.
So is it possible to define such a filter structure in the ASP.net and make the corresponding logical operation in the filter? The answer is yes, and this article will tell you how to configure your Web application to IIS if you write a filter.
Process one: How to write a filter
Writing a filter is actually writing a filter class, that is, writing a HttpModule module, this filter should implement IHttpModule base class, and rewrite the Init method, give you a practical example as follows:
This is a PageFilter.cs
Copy Code code as follows:
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";}
}
Registering HttpApplication in the Init method
Registering an event by way of a delegate
public void Init (HttpApplication application)
{
Application. AcquireRequestState + = new EventHandler (application_acquirerequeststate);
}
private void Application_acquirerequeststate (Object source, EventArgs E)
{
HttpApplication application = (HttpApplication) source;
HttpContext context = Application. context;
HttpSessionState session = Context. session;
HttpRequest request = context. Request;
HttpResponse response = context. Response;
String ContextPath = Request. Applicationpath;
}
}
It should be explained that "filters" can also be called "interceptors", the process of intercepting the entire HTTP request/response, because the entire request/response process can be divided into many stages, then this involves a problem, which is your filter to intercept the specific phase, the above init function, You can define the specific stage you want to intercept, for example, the above interception is the stage to generate the request session, Acquirerequeststat is the representative of this state, and the corresponding processing function is application_acquirerequeststate after interception, So the following defines a application_acquirerequeststate method in which you can obtain a series of objects such as application, context, session, request, response, etc. by force type conversion. , on the basis of acquiring these objects, you can write the core business logic, such as getting a sense of whether the current URL access is legitimate, checking whether the current access is a user's access after login, 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 above in Init, you can define the following logic:
Application. Phase of the standard name 1 + = new EventHandler (this phase of the corresponding processing method name 1);
Application. Phase of the standard name 2 + = new EventHandler (this phase of the corresponding processing method name 2);
。。。
The standard name of the stage is that these stages are of the standard name and are standard attributes of the application object, such as the above AcquireRequestState, as well as the beginrequest, AuthenticateRequest, AuthorizeRequest, Resolverequestcache, AcquireRequestState, PreRequestHandlerExecute, PostRequestHandlerExecute, ReleaseRequestState, Updaterequestcache, endrequest many stages, and so on, these stages have a specific meaning.
This phase corresponds to the name of the processing method, in fact, you define the corresponding phase of the processing method, there are examples, no longer explain.
There is also a need to pay special attention, there are so many stages can intercept, but in practical applications, we often intercept the one or two stages, but also to note that some server objects only at a specific stage can be intercepted, such as the session object in the BeginRequest phase is not, In the AcquireRequestState and its later stage is there, so in accordance with the actual demand to intercept the specific stage, this is the novice most likely to encounter problems.
Procedure two: How to configure Filtering
We have prepared a filter for the. cs file, so how to make this filter work, this need to configure, the default is certainly not intercepted, you need to configure the filter to the application of the Web.config file, the above sample configuration is as follows:
Copy Code code as follows:
<configuration>
<system.web>
<add name= "Pagemodule" type= "Pagefilter"/>
</system.web>
</configuration>
This is actually configured, then publish the site to build a DLL, and so on, will automatically intercept URL access, but remember that by default, all requests for the application will be intercepted, if you point to intercept a specific request, such as to intercept only the ASPX file request, Then you can add a decision to the file suffix name in the filter logic, if not the ASPX direct pass