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 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. To implement this interface, there are two methods, one is Dispose and the other is Init. The parameter of Init is an instance of HttpApplication type. Use this instance to register some events. Since the URL is now filtered, the AcquireRequestState event is registered. Similar events are listed as follows
#To obtain the url to be redirected, you can use the CurrentExecutionFilePath attribute of the request, and to obtain the suffix of the requested file, you can use the CurrentExecutionFilePathExtension. As for what rules to judge, Depends on demand. What I am doing here is to determine whether the Session exists during the request. If it does not exist, it will jump back to the login page. Since Session is used, an exception will be thrown when opening the page. The exception message is "Session state is not available in this context.". After implementing the IRequiresSessionState interface, no exception will be thrown.
In addition, you also need to add the following code under the
<httpModules> <add name="filters" type="QTJZ.Filters"/> </httpModules>
The type attribute is the fully qualified name of the above Filters class
For more articles related to URL filtering implementation code in ASP.NET, please pay attention to the PHP Chinese website!