How to implement a custom method using an event in a request pipeline

Source: Internet
Author: User

1. Create a new class Xx.cs:IHttpModule, inherit the interface, implement the interface method

public classValidatesessionhttpmodule:ihttpmodule {public voidDispose () {throw new notimplementedexception (); }///<summary>//Complete the registration of events in the request pipeline///</summary>//<param name= "context" >&L t;/param> public void Init (HttpApplication context) {//throw New NotImplementedException (            ); The event registration method that executes the context when the event is triggered.        AcquireRequestState + = context_acquirerequeststate; } private void Context_acquirerequeststate (object sender, EventArgs e) {//throw new notimplementedexception (); HttpApplication application = sender as HttpApplication; HttpContext context = application. Context; String url = context. Request.Url.ToString (); if (URL. Contains ("Admin")) {if (context. session["key"] = = null) {context. Response.Redirect ("/login.aspx");}} } }

2. or write your own code in the corresponding method of the global file

public classGlobal:System.Web.HttpApplication {//<summary>///The method that is called when the Web application first starts, and is only called once///&LT        ;/summary>//<param name= "sender" ></param>///<param name= "E" ></param> protected void Application_Start (objectsender, EventArgs e) {#region timed task frame #endregion}///<summary>///session start call, can be used to count total visitors through application///</summary>//<param Name= "Sender" ></param>//<param name= "E" ></param> protected void Session_Start (OBJEC Tsender, EventArgs e) {} protected void Application_BeginRequest (Object sender, EventArgs e) {} protect ed void Application_AuthenticateRequest (Object sender, EventArgs e) {}///<summary>//Global exception handling point if there are TRYCAC in the program He does not call this method//</summary>//<param name= "sender" ></param>//<param name= "E" ></param> protected void Application_Error (Object sender, EventArgs e) {//Get exception information Exception ex = HttpContext.Current.Server . GetLastError (); Log4net write log } protected void Session_End (Object sender, EventArgs e) {} protected void Application_End (Object
             
               sender, EventArgs e) {}} 
                   

How to implement a custom method using an event in a request pipeline

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.