C# 在asp.net中global.asax的目的是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2340572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the purpose of global.asax in asp.net
提问by Harikrishna
How can we use global.asax in asp.net? And what is that?
我们如何在asp.net中使用global.asax?那是什么?
采纳答案by Rob
MSDN has an outlineof the purpose of the global.asax file.
MSDN 概述了 global.asax 文件的用途。
Effectively, global.asax allows you to write code that runs in response to "system level" events, such as the application starting, a session ending, an application error occuring, without having to try and shoe-horn that code into each and every page of your site.
有效地,global.asax 允许您编写响应“系统级”事件(例如应用程序启动、会话结束、应用程序错误发生)而运行的代码,而无需尝试将该代码硬塞到每个您网站的页面。
You can use it by by choosing Add > New Item > Global Application Class in Visual Studio. Once you've added the file, you can add code under any of the events that are listed (and created by default, at least in Visual Studio 2008):
您可以通过在 Visual Studio 中选择添加 > 新建项 > 全局应用程序类来使用它。添加文件后,您可以在列出的任何事件下添加代码(默认情况下创建,至少在 Visual Studio 2008 中):
- Application_Start
- Application_End
- Session_Start
- Session_End
- Application_BeginRequest
- Application_AuthenticateRequest
- Application_Error
- 申请_开始
- 申请_结束
- 会话_开始
- 会话结束
- Application_BeginRequest
- Application_AuthenticateRequest
- 应用程序错误
There are other events that you can also hook into, such as "LogRequest".
您还可以挂钩其他事件,例如“LogRequest”。
回答by Matt
The Global.asax can be used to handle events arising from the application. This link provides a good explanation: http://aspalliance.com/1114
Global.asax 可用于处理应用程序产生的事件。此链接提供了很好的解释:http: //aspalliance.com/1114
回答by Shoban
The Global.asax file, also known as the ASP.NET application file, is an optional file that contains code for responding to application-level and session-level events raised by ASP.NET or by HTTP modules.
Global.asax 文件,也称为 ASP.NET 应用程序文件,是一个可选文件,其中包含用于响应由 ASP.NET 或 HTTP 模块引发的应用程序级和会话级事件的代码。
回答by Oded
Global.asaxis the asp.net application file.
Global.asax是asp.net 应用程序文件。
It is an optional file that handles events raised by ASP.NET or by HttpModules. Mostly used for application and session start/end events and for global error handling.
它是一个可选文件,用于处理由 ASP.NET 或 HttpModules 引发的事件。主要用于应用程序和会话开始/结束事件以及全局错误处理。
When used, it should be in the root of the website.
使用时,它应该在网站的根目录中。
回答by Nithin Paul
Global asax events explained
全球 asax 事件解释
Application_Init:Fired when an application initializes or is first called. It's invoked for all HttpApplication object instances.
Application_Init:当应用程序初始化或第一次调用时触发。它为所有 HttpApplication 对象实例调用。
Application_Disposed:Fired just before an application is destroyed. This is the ideal location for cleaning up previously used resources.
Application_Disposed:在应用程序被销毁之前触发。这是清理以前使用过的资源的理想位置。
Application_Error:Fired when an unhandled exception is encountered within the application.
Application_Error:在应用程序中遇到未处理的异常时触发。
Application_Start:Fired when the first instance of the HttpApplication class is created. It allows you to create objects that are accessible by all HttpApplication instances.
Application_Start:在创建 HttpApplication 类的第一个实例时触发。它允许您创建所有 HttpApplication 实例都可以访问的对象。
Application_End:Fired when the last instance of an HttpApplication class is destroyed. It's fired only once during an application's lifetime.
Application_End:当 HttpApplication 类的最后一个实例被销毁时触发。它在应用程序的生命周期内仅被触发一次。
Application_BeginRequest:Fired when an application request is received. It's the first event fired for a request, which is often a page request (URL) that a user enters.
Application_BeginRequest:收到应用程序请求时触发。这是为请求触发的第一个事件,请求通常是用户输入的页面请求 (URL)。
Application_EndRequest:The last event fired for an application request.
Application_EndRequest:为应用程序请求触发的最后一个事件。
Application_PreRequestHandlerExecute:Fired before the ASP.NET page framework begins executing an event handler like a page or Web service.
Application_PreRequestHandlerExecute:在 ASP.NET 页面框架开始执行事件处理程序(如页面或 Web 服务)之前触发。
Application_PostRequestHandlerExecute:Fired when the ASP.NET page framework is finished executing an event handler.
Application_PostRequestHandlerExecute:当 ASP.NET 页面框架完成执行事件处理程序时触发。
Applcation_PreSendRequestHeaders:Fired before the ASP.NET page framework sends HTTP headers to a requesting client (browser).
Applcation_PreSendRequestHeaders:在 ASP.NET 页面框架将 HTTP 标头发送到请求客户端(浏览器)之前触发。
Application_PreSendContent:Fired before the ASP.NET page framework sends content to a requesting client (browser).
Application_PreSendContent:在 ASP.NET 页面框架将内容发送到请求客户端(浏览器)之前触发。
Application_AcquireRequestState:Fired when the ASP.NET page framework gets the current state (Session state) related to the current request.
Application_AcquireRequestState:当 ASP.NET 页面框架获取与当前请求相关的当前状态(会话状态)时触发。
Application_ReleaseRequestState:Fired when the ASP.NET page framework completes execution of all event handlers. This results in all state modules to save their current state data.
Application_ReleaseRequestState:当 ASP.NET 页面框架完成所有事件处理程序的执行时触发。这导致所有状态模块保存其当前状态数据。
Application_ResolveRequestCache:Fired when the ASP.NET page framework completes an authorization request. It allows caching modules to serve the request from the cache, thus bypassing handler execution.
Application_ResolveRequestCache:当 ASP.NET 页面框架完成授权请求时触发。它允许缓存模块为来自缓存的请求提供服务,从而绕过处理程序的执行。
Application_UpdateRequestCache:Fired when the ASP.NET page framework completes handler execution to allow caching modules to store responses to be used to handle subsequent requests.
Application_UpdateRequestCache:当 ASP.NET 页面框架完成处理程序执行以允许缓存模块存储用于处理后续请求的响应时触发。
Application_AuthenticateRequest:Fired when the security module has established the current user's identity as valid. At this point, the user's credentials have been validated.
Application_AuthenticateRequest:当安全模块确定当前用户的身份有效时触发。此时,用户的凭据已被验证。
Application_AuthorizeRequest:Fired when the security module has verified that a user can access resources.
Application_AuthorizeRequest:当安全模块验证用户可以访问资源时触发。
Session_Start:Fired when a new user visits the application Web site.
Session_Start:当新用户访问应用程序网站时触发。
Session_End:Fired when a user's session times out, ends, or they leave the application Web site.
Session_End:当用户的会话超时、结束或离开应用程序网站时触发。
回答by sandeep mvn
The root directory of a web application has a special significance and certain content can be present on in that folder. It can have a special file called as “Global.asax”. ASP.Net framework uses the content in the global.asax and creates a class at runtime which is inherited from HttpApplication. During the lifetime of an application, ASP.NET maintains a pool of Global.asax derived HttpApplication instances. When an application receives an http request, the ASP.Net page framework assigns one of these instances to process that request. That instance is responsible for managing the entire lifetime of the request it is assigned to and the instance can only be reused after the request has been completed when it is returned to the pool. The instance members in Global.asax cannot be used for sharing data across requests but static member can be. Global.asax can contain the event handlers of HttpApplication object and some other important methods which would execute at various points in a web application
Web 应用程序的根目录具有特殊意义,该文件夹中可以存在某些内容。它可以有一个名为“Global.asax”的特殊文件。ASP.Net 框架使用 global.asax 中的内容并在运行时创建一个继承自 HttpApplication 的类。在应用程序的生命周期内,ASP.NET 维护一个 Global.asax 派生的 HttpApplication 实例池。当应用程序收到一个 http 请求时,ASP.Net 页面框架会分配这些实例之一来处理该请求。该实例负责管理分配给它的请求的整个生命周期,并且该实例只能在请求完成后返回到池中时才能重用。Global.asax 中的实例成员不能用于跨请求共享数据,但静态成员可以。全球的。