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///< ;/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