Asp. NET URL Filtering implementation code

Source: Internet
Author: User
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!
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.