URL filtering implementation code in ASP.NET

高洛峰
Release: 2017-01-21 15:11:56
Original
2249 people have browsed it

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");
                 }
             }
         }
     }
 }
Copy after login

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

URL filtering implementation code in ASP.NET

#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 node of the configuration file Web.config

<httpModules> 
<add name="filters" type="QTJZ.Filters"/> 
</httpModules>
Copy after login

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!
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template