The following is the definition of the class.
Copy Code code as follows:
Using System;
Using System.Web;
Using System.Web.SessionState;
Namespace QTJZ
{
public class Filters:ihttpmodule, IRequiresSessionState
{
public void Dispose () {}
public void Init (HttpApplication application)
{
Application. AcquireRequestState + = new EventHandler (application_acquirerequeststate);
}
public void Application_acquirerequeststate (object sender, EventArgs e)
{
HttpApplication application = sender as HttpApplication;
HttpRequest request = Application. Request;
HttpResponse response = Application. Response;
String url=request. Currentexecutionfilepath.trim ('/');
string suffix = Request. Currentexecutionfilepathextension.trim ('. ');
if (!url. Equals ("default.htm") && (suffix = = "aspx" | | | suffix = = "htm"))
{
Object sessionobj = Application. Context.session = null? Null:application. session["Useid"];
if (sessionobj==null)
{
Response. Redirect ("~/default.htm");
}
}
}
}
}
In order to achieve the filtering effect, the filters class needs to implement the Ihttpmoeld interface, which requires two methods, one is dispose and the other is init. The init parameter is an instance of a HttpApplication type that uses this instance to register some events. Because the URL is now being filtered, the AcquireRequestState event is registered. Similar events are listed below
| BeginRequest |
Occurs as the first event in the chain of execution of a pipeline as HTTP when ASP.net responds to a request. |
| AuthenticateRequest |
Occurs when the security module has established a user identity. |
| AuthorizeRequest |
Occurs when a security module has authenticated a user authorization. |
| Resolverequestcache |
Occurs after the asp.net completes the authorization event so that the cache module is serviced from the cache for the request.
This bypasses the execution of event handlers, such as a page or XML Web services. |
| AcquireRequestState |
Occurs when ASP.net gets the current state, such as session state, that is associated with the current request. |
| PreRequestHandlerExecute |
Occurs just before ASP.net begins executing an event handler, such as a page or an XML Web services. |
| PostRequestHandlerExecute |
Occurs when a ASP.net event handler (for example, a page or an XML Web service) finishes execution. |
| ReleaseRequestState |
Occurs after all request event handlers have been executed by ASP.net. This event causes the state module to save the current state data. |
| Updaterequestcache |
Occurs when the ASP.net finishes executing an event handler so that the cache module store will be used to provide a service from the cache for subsequent requests. |
| EndRequest |
Occurs as the last occurrence of an HTTP execution pipeline chain when ASP.net responds to a request. |
Get the URL to jump can use the Currentexecutionfilepath attribute of request, and get the suffix of the requested file can be used currentexecutionfilepathextension, as to what rules to judge, Depends on the demand. I am here at the request to determine whether the session exists, does not exist to jump back to the landing page. Because you want to use the session, an exception is thrown when the page is opened, and the exception message is "The conversation state is not available in this context." ", no exception was thrown after the IRequiresSessionState interface was implemented.
In addition, you need to add the following code under the <system.web> node of the configuration file web.config
Copy Code code as follows:
<add name= "Filters" type= "QTJZ". Filters "/>
Where the Type property is the fully qualified name of the filters class above