Description of Global. asax, global. asax
Global. asax is our underlying file. The first IIS request will first execute the file in it, so it is necessary to learn the functions in it. In addition, we always ignore the knowledge points here and always feel that this is not necessary. In fact, we are wrong. Here is the root of the program.
File Code:
/// <Summary> /// all applications, status, program accessed, and user exited can be found ., /// </Summary> public class Global: System. web. httpApplication {/// <summary> /// the IIS request is executed once at the beginning. There is also an Application_End method. /// </Summary> // This function is executed once when the program is started, which is equivalent to the Main function, /// <param name = "sender"> </param> // <param name = "e"> </param> protected void Application_Start (object sender, EventArgs e) {File. appendAllText ("c:/1.txt", DateTime. now. toString () + "Application_Start");} // <summary> // The Session here is a temporary storage space opened by the server for each browser to store data, when the browser is closed or the user switches over, the memory will be re-opened to save the Session // Each browser's webpage will have a Session // a user in the browser will share a Session, when using When the user voluntarily exits, /// </summary> // counts the number of online users /// <param name = "sender"> </param> /// <param name = "e"> </param> protected void Session_Start (object sender, eventArgs e) {// This is equivalent to creating a temporary session. HttpContext. Current. Session. Abandon (); // destroy the Session and automatically cancel the Session within 15 minutes. /// <summary> // process the request. (Every application will be triggered here) /// </summary> /// <param name = "sender"> </param> /// view the URL of the current request, through this (HttpContext. current. request. URL) in the // quick listener, view the URLs requested in total /// <param name = "e"> </param> protected void Application_BeginRequest (object sender, EventArgs e) {// implementation function, shielding IP if (HttpContext. current. request. userHostAddress = "192.168.1.102") {HttpContext. current. response. write ("here you can block the IP address of your computer. ");} Else {HttpContext. current. response. write ("") ;}// anti-theft hunting} protected void Application_AuthentiateRequest (object sender, EventArgs e) {}/// <summary> /// Exception Handling Module /// </summary> /// call the function here to expose the exception information /// <param name = "sender"> </param> // <param name = "e"> </param> protected void Application_Error (object sender, eventArgs e) {}/// <summary> // disconnect the session. This is called at 15 times out /// </summary> /// <param name = "sender"> </Param> // <param name = "e"> </param> protected void Session_End (object sender, EventArgs e) {}/// <summary> /// the program is executed once when it is disabled, and IIS is executed only when it is disabled. /// </Summary> /// <param name = "sender"> </param> /// <param name = "e"> </param> protected void Application_End (object sender, eventArgs e ){}}
Function Description:
The Global. asax file is a must-have file for IIS request execution and is very important. For a Web application, it is an event response in a declaration cycle. Global class, which inherits from System. Web. HttpApplication. It defines methods, attributes, and events common to all application objects in ASP. NET applications. This class is the base class of the application defined in the Global. asax file. *: Application_Start () This function is called when our program was started. It is the same as the Main function in C language. It is all the functions executed at the beginning of the program, you can write some functions and methods to be initialized here, such as routing, logs, IOC, DI, region, and files. When the function is disabled, there is a corresponding method Application_End () function.
protected void Application_Start() { EngineContext.Initialize(false); var dependencyResolver = new ControllerDependencyResolver(); DependencyResolver.SetResolver(dependencyResolver); GlobalConfiguration.Configuration.DependencyResolver = dependencyResolver; AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); }
protected void Application_End(object sender,EventArgs e) { }
*: Application_Error () exception handling function. When an uncaptured exception occurs in the application, this function is called. HttpContext is used here. current. server. getLastError () to obtain the exception information. You can save it to the log4Net record to log.
protected void Application_Error(object sender, EventArgs e) { var exception = Server.GetLastError(); LogException(exception); }
*: Session_Start () Server Session information. Here, when each browser accesses the server, the server creates a memory space for it, that is, the Session, to save some of its information, some of the web pages we open are accessed in a Session. (It seems that a thread is automatically dropped within 15 minutes, which is equivalent to a session with a limited time .) A user in a browser shares a Session. When the user exits, the Session is closed. Call the following function to close the Session, Session_End (). It can be used to count the number of online users.
HttpContext. Current. Session. Abandon (); // destroy the Session, which is automatically canceled within 15 minutes
Protected void Session_Start (object sender, EventArgs e) {// This is equivalent to creating a temporary session. HttpContext. Current. Session. Abandon (); // destroy Session, automatically canceled in 15 minutes}
protected void Session_End(object sender,EventArgs e) { }
*: This function is accessed when Application_BeginRequest () is requested. Each application triggers this function. We can use the following function to view the current request URL (HttpContext. current. request. URL ). You can view it in the quick monitor. The total number of requests for a Web page. The IP address and anti-theft image hunting functions can be shielded here.
Protected void Application_BeginRequest (object sender, EventArgs e) {// implement the function to shield IP if (HttpContext. current. request. userHostAddress = "192.168.1.102") {HttpContext. current. response. write ("here you can block the IP address of your computer. ");} Else {HttpContext. Current. Response. Write (" ");} // anti-theft hunting}
*: I am not very clear about this function. Please complete it later.
protected void Application_AuthentiateRequest(object sender,EventArgs e) { }