Application of ASP.NET filter is introduced

  • 2020-06-12 08:51:19
  • OfStack

In J2EE Web developing filter filter, the filter can access to the specified URL to intercept, and perform the filter method, based on the actual application, modify the request in the filter code, judgment, session information, also can do access control, anyhow this filter makes sense, also can saying is the chain of responsibility design pattern in J2EE 1 application.

Is it possible to define such a filter structure in ES11en.NET and do the corresponding logical operations in the filter? The answer is yes, and this article will show you how to configure Web for IIS if you write a filter.

Process 1: How do You write filters

To write a filter is to write a class with a filter, that is to write an HttpModule module. This filter should implement the IHttpModule base class and override the Init method.

This is 1 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"; }
        }
        // in  Init  Method registration HttpApplication 
        //  Register events by 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;
        }
}

To be sure, "filter" can also be referred to as "interceptor", namely to intercept the HTTP request/response process, because the request/response process can be divided into many stage, then it will be involved in a problem, that is your filters which one want to intercept a specific phase, the above Init function, can define your own want to intercept the specific stage, for example, is the intercept request the session stage, AcquireRequestStat is the representative of the state and the corresponding processing function for Application_AcquireRequestState after interception, So below defines a Application_AcquireRequestState method, in this way can gain application casts, context, session, request, response 1 series such as object, on the basis of access to these objects, you'll see for the preparation of the core business logic, such as obtaining whether current URL access to legal, check whether the current access for the logged in user's access and so on.

And since the whole process of interception has many phases, how do you intercept the other phases? This should be very simple, similar to the above in Init according to the following logic definition:

application. Standard name 1 += new EventHandler(name 1 of the processing method corresponding to this stage);
application. Standard name 2 += new EventHandler(name 2 of the process for this stage);
.

A standard name for a stage means that the stage has a standard name and is a standard attribute of the application object, such as AcquireRequestState above, as well as many stages such as BeginRequest, AuthenticateRequest, AuthorizeRequest, ResolveRequestCache, AcquireRequestState, PreRequestHandlerExecute, PostRequestHandlerExecute, ReleaseRequestState, UpdateRequestCache, EndRequest, etc., all of which have specific meanings.

The name of the processing method corresponding to this stage is actually your own definition of the processing method corresponding to this stage.

One point need to pay special attention to, there are so many stages can intercept, but in practical application, are 1 we intercepted two stages, and pay attention to some of the server object only in a specific phase can be intercepted, such as Session object is not in the BeginRequest stage, there is in the stage and after AcquireRequestState, so according to the actual demand to intercept a specific stage, this is the most likely to encounter new problems.

Process 2: How are filters configured

We have written a filter for.cs file, so how to make this filter work, this needs to be configured, the default is certainly not intercepted, you need to configure the filter to the application of Web.config file, the above sample configuration is as follows:


<configuration>
<system.web>
 <httpModules>
   <add name="pageModule" type="PageFilter"/>
 </httpModules>
</system.web>
</configuration>

So is configured, then release sites generated dll etc, then automatically intercept URL access, but 1 point to remember is that by default for the application of all requests will be intercepted, if you point to intercept a particular request, for example, only want to request for aspx file to intercept, you can add to the file suffix in the filter logic judgment, if not aspx directly pass


Related articles: