The following is the definition of the class.
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 AP Plication = 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"); } } } } }
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 the HttpApplication type that registers some events with this instance. Because the URL is now filtered, the AcquireRequestState event is registered. Similar events are listed below
Get the URL to jump can use the Currentexecutionfilepath property of request, and get the suffix of the requested file can be used currentexecutionfilepathextension, as to what rules to judge, Depends on demand. I am here at the request to determine whether the session exists, does not exist then jump back to the landing page. Because the session is to be used, an exception is thrown when the page is opened, and the exception message is "The state is not available in this context." "No exception was thrown when 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
Where the type attribute is the fully qualified name of the filters class above
more URL Filtering implementation code in ASP. Related articles please follow topic.alibabacloud.com!