Asp. NET Global Application Classes
from the old man's blog http://shijiaqi1066.iteye.com/blog/1728603
This is saved to your blog inside to learn to see later
Global file
New Global file
Visual Studio Add New Item--"Web"--"Global Application Class"
produces the global class.
A webapplication can have only one global application class. If the new discovery does not have a global application class, it means that the class already exists, and if you need to create a new, delete the old ones before creating them.
Global class:
C # code
- Using System;
- Using System.Collections.Generic;
- Using System.Linq;
- Using System.Web;
- Using System.Web.Security;
- Using System.Web.SessionState;
- Namespace WebApplication1
- {
- public class Global:System.Web.HttpApplication
- {
- protected void Application_Start (object sender, EventArgs e)
- {
- //code that runs when the application starts.
- //The method is executed the first time the program obtains the request.
- }
- protected void session_start (object sender, EventArgs e)
- {
- //code that runs when a new session is started.
- //The method is called, indicating that there is currently a new session generated.
- }
- protected void Application_BeginRequest (object sender, EventArgs e)
- {
- //code that is allowed when a new request is started.
- //Each request triggers the method.
- }
- protected void Application_AuthenticateRequest (object sender, EventArgs e)
- {
- //code that runs when the application starts.
- //The method is executed the first time the program obtains the request.
- }
- protected void Application_Error (object sender, EventArgs e)
- {
- //code that runs when an unhandled error occurs
- //Get exception information and process: HttpContext.Current.Server.GetLastError ();
- }
- protected void Session_End (object sender, EventArgs e)
- {
- //code that runs at the end of the session.
- //Note: The Session_End event is raised only if the sessionstate mode in the Web. config file is set to InProc.
- //If the session mode is set to StateServer or SQL Server, the event is not raised.
- }
- protected void Application_End (object sender, EventArgs e)
- {
- //code to run when the application shuts down
- }
- }
- }
Application:
Session_Start () Method: Count the number of people online.
Application_BeginRequest () Method: Block IP, prevent hotlinking. Yes, so the picture is watermark.
Application_AuthenticateRequest () Method: Validation method.
Application_Error () Method:
Add:
1. Get the current request URL:HTTPCONTEXT.CURRENT.REQUEST.URL
2. Manual Logoff Session:HttpContext.Current.Session.Abandon ()
3. Add text information to the file and create it first if the file does not exist: File.appendalltext ("C:1.txt", DateTime.Now.ToString ())
4. Ip:HttpContext.Current.Request.UserHostAddress of requests for access to the website;
5. Print output to the page: HttpContext.Current.Request.Write ("has been blocked! ");
6. Print output end to call: HttpContext.Current.Request.End ();
7. Get Request Type: HttpContext.Current.Request.ContentType
8. Get URL information for client last request: HttpContext.Current.Request.UrlReferrer; (Description: This property is an instance of the URI class.) )
9. Gets the domain name of the URI object: URI. Host;
(Gets the domain name of the URL that the client last requested: HttpContext.Current.Request.UrlReferrer.Host;)
10. Get exception information: HttpContext.Current.Server.GetLastError ();
ASP. NET global Application class