【ASP.NET】Global.asax与Web.config
2018年08月28日 09:30:05 像风走过八千里 阅读数 844
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/cxh6863/article/details/82142390
背景
在创建网站的项目,总是会看到Web.config这个文件,它是用来干什么的呢?还有咱们自己也可以新建Global.asax文件,它是用来干什么的呢。在我实现一个网页上显示历史访问人数和在线人数的时候,有一种Global.asax文件和Web.config文件相似的错觉。所以就在这里详细了解一下。
Global.asax
Global.asax是一个全局文件,一个ASP.NET的应用程序文件,是从从HttpApplication基类派生的类。
响应的是应用程序级别和会话级别事件
当需要处理应用程序事件或会话事件时,可建立使用Global.asax文件
包含的事件
1、在HttpApplication 类的第一个实例被创建时,该事件被触发。
它允许你创建可以由所有HttpApplication 实例访问的对象。
这是在应用程序中给应用程序级的变量赋值或指定对所有用户必须保持的状态的理想位置。(应用程序启动时,执行。)
void Application_Start (object sender, EventArgs e){
}
2、在一个新用户访问应用程序 Web 站点时,该事件被触发。(会话开始时,执行)
<span style="color:#000000"><code><span style="color:#000088 !important">protected</span> <span style="color:#000088 !important">void</span> <span style="color:#009900 !important">Session_Start</span>(<span style="color:#000088 !important">object</span> sender, EventArgs e)
{
}</code></span>
- 1
- 2
- 3
3、在接收到一个应用程序请求时触发。对于一个请求来说,它是第一个被触发的事件,请求一般是用户输入的一个页面请求(URL)
protected void Application_BeginRequest(object sender, EventArgs e) {
}
4、在安全模块建立起当前用户的有效的身份时,该事件被触发。在这个时候,用户的凭据将会被验证。
<span style="color:#000000"><code><span style="color:#000088 !important">protected</span> <span style="color:#000088 !important">void</span> <span style="color:#009900 !important">Application_AuthenticateRequest</span>(<span style="color:#000088 !important">object</span> sender, EventArgs e)
{
}</code></span>
5、当应用程序中遇到一个未处理的异常时,该事件被触发。
<span style="color:#000000"><code><span style="color:#000088 !important">protected</span> <span style="color:#000088 !important">void</span> <span style="color:#009900 !important">Application_Error</span>(<span style="color:#000088 !important">object</span> sender, EventArgs e)
{
<span style="color:#880000 !important">//在出现未处理的错误时运行的代码</span>
}</code></span>
6、在 InProc 模式下运行时,当一个用户的会话超时、结束或他们离开应用程序 Web 站点时,该事件被触发。(会话结束或过期时,执行)
protected void Session_End (object sender, EventArgs e){
}
7、在HttpApplication 类的最后一个实例被销毁时,该事件被触发。在一个应用程序的生命周期内它只被触发一次。应用程序关闭时,该事件被触发。(应用程序结束时,执行)
protected void Application_End (object sender, EventArgs e){
}
- 1
- 2
- 3
Web.config
Web.config是一个配置文件,是基于XML的文本文件。
在发布Web应用程序时,Web.config文件并不编译进dll文件中,将来有变化时,可直接用记事本打开Web.config文件进行编辑修改,很方便。
包含的结点
1、appSettings节点:主要用来存储asp.net应用程序的一些配置信息,比如上传文件的保存路径等
<span style="color:#000000"><code><span style="color:#006666 !important"><<span style="color:#4f4f4f !important">appSettings</span>></span>
<span style="color:#006666 !important"><<span style="color:#4f4f4f !important">add</span> <span style="color:#4f4f4f !important">key</span>=<span style="color:#009900 !important">"***"</span> <span style="color:#4f4f4f !important">value</span>=<span style="color:#009900 !important">"***"</span>/></span>
<span style="color:#006666 !important"></<span style="color:#4f4f4f !important">appSettings</span>></span></code></span>
- 1
- 2
- 3
2、connectionStrings节点:主要用于配置数据库连接的
<span style="color:#000000"><code><span style="color:#006666 !important"><<span style="color:#4f4f4f !important">connectionStrings</span>></span>
<span style="color:#880000 !important"><!--SQL Server数据库配置--></span>
<span style="color:#006666 !important"><<span style="color:#4f4f4f !important">add</span> <span style="color:#4f4f4f !important">name</span>=<span style="color:#009900 !important">"***"</span> <span style="color:#4f4f4f !important">connectionString</span>=<span style="color:#009900 !important">"Data Source=***;Initial Catalog=****;User ID=***;Password=***"</span>/></span>
<span style="color:#006666 !important"></<span style="color:#4f4f4f !important">connectionStrings</span>></span></code></span>
- 1
- 2
- 3
- 4
代码中的对应
<span style="color:#000000"><code><span style="color:#880000 !important">//读取web.config节点配置 </span>
<span style="color:#000088 !important">string</span> connectionString = ConfigurationManager.ConnectionStrings[<span style="color:#009900 !important">"***"</span>].ConnectionString;
<span style="color:#880000 !important">//实例化SqlConnection对象 </span>
SqlConnection connection = <span style="color:#000088 !important">new</span> SqlConnection(connectionString);</code></span>
- 1
- 2
- 3
- 4
3、compilation节点:配置 ASP.NET 使用的所有编译设置,。默认的debug属性为“true”,即允许调试。交付使用后应设为false。
<span style="color:#000000"><code><span style="color:#880000 !important">//允许调式</span>
<span style="color:#4f4f4f !important"><</span>compilation debug<span style="color:#4f4f4f !important">=</span><span style="color:#009900 !important">"true"</span><span style="color:#4f4f4f !important">></span>
......
<span style="color:#4f4f4f !important"><</span>/compilation<span style="color:#4f4f4f !important">></span></code></span>
- 1
- 2
- 3
- 4
4、authentication节点:设置asp.net身份验证模式,有四种。
mode属性:
身份验证类型 | 描述 |
---|---|
Windows | 默认的身份验证形式,用于任何形式的IIS身份验证。Windows身份验证,适用于域用户或者局域网用户 |
Forms | 基于ASP.NET窗体的身份验证作为默认的身份验证模式。表单验证,依靠网站开发人员进行身份验证 |
Passport | Microsoft Passport身份验证作为默认的身份验证模式。使用微软提供的身份验证服务进行身份验证。 |
None | 没有身份验证。用户匿名用户和可以提供其自己的身份验证的应用程序 |
<span style="color:#000000"><code><span style="color:#006666 !important"><<span style="color:#4f4f4f !important">authentication</span> <span style="color:#4f4f4f !important">mode</span>=<span style="color:#009900 !important">"Windows|Forms|Passport|None"</span>></span>
<span style="color:#006666 !important"></<span style="color:#4f4f4f !important">authentication</span>></span></code></span>
- 1
- 2
5、customErrors节点:用于定义一些自定义错误信息的信息
defaultRedirect属性(可选):
表示应用程序发生错误时重定向到的默认URL,如果没有指定该属性则显示一般性错误
Mode属性(必选):
(1)On 表示在本地和远程用户都会看到自定义错误信息。
(2)Off 禁用自定义错误信息,本地和远程用户都会看到详细的错误信息。
(3)RemoteOnly 表示本地用户将看到详细错误信息,而远程用户将会看到自定义错误信息。
<span style="color:#000000"><code><span style="color:#006666 !important"><<span style="color:#4f4f4f !important">customErrors
</span> <span style="color:#4f4f4f !important">defaultRedirect</span>=<span style="color:#009900 !important">"url"</span>
<span style="color:#4f4f4f !important">mode</span>=<span style="color:#009900 !important">"On|Off|RemoteOnly"</span>></span>
<span style="color:#006666 !important"><<span style="color:#4f4f4f !important">error</span> <span style="color:#4f4f4f !important">statusCode</span>=<span style="color:#009900 !important">"statuscode"</span> <span style="color:#4f4f4f !important">redirect</span>=<span style="color:#009900 !important">"url"</span>/></span>
<span style="color:#006666 !important"></<span style="color:#4f4f4f !important">customErrors</span>></span></code></span>
- 1
- 2
- 3
- 4
- 5
error子节点:重定向到我们自定义的错误页面
6、httpHandlers节点:于根据用户请求的URL和HTTP谓词将用户的请求交给相应的处理程序。可以针对某个特定目录下指定的特殊文件进行特殊处理。可以在配置级别的任何层次配置此节点
<span style="color:#000000"><code><span style="color:#006666 !important"><<span style="color:#4f4f4f !important">httpHandlers</span>></span>
//禁止访问aaa目录下的任何txt文件
<span style="color:#006666 !important"><<span style="color:#4f4f4f !important">add</span> <span style="color:#4f4f4f !important">path</span>=<span style="color:#009900 !important">"aaa/*.txt"</span> <span style="color:#4f4f4f !important">verb</span>=<span style="color:#009900 !important">"*"</span> <span style="color:#4f4f4f !important">type</span>=<span style="color:#009900 !important">"System.Web.HttpForbiddenHandler"</span> <span style="color:#4f4f4f !important">validate</span>=<span style="color:#009900 !important">"true"</span>/></span>
<span style="color:#006666 !important"></<span style="color:#4f4f4f !important">httpHandlers</span>></span></code></span>
- 1
- 2
- 3
- 4
7、sessionState节点:用于配置当前asp.net应用程序的会话状态配置
mode属性:
属性值 | 说明 |
---|---|
Custom | 使用自定义数据来存储会话状态数据。 |
InProc | 默认值。由asp.net辅助进程来存储会话状态数据。 |
Off | 禁用会话状态。 |
SQLServer | 使用进程外SQL Server数据库保存会话状态数据。 |
<span style="color:#000000"><code><span style="color:#006666 !important"><<span style="color:#4f4f4f !important">sessionState</span> <span style="color:#4f4f4f !important">cookieless</span>=<span style="color:#009900 !important">"false"</span> <span style="color:#4f4f4f !important">mode</span>=<span style="color:#009900 !important">"InProc"</span> <span style="color:#4f4f4f !important">timeout</span>=<span style="color:#009900 !important">"30"</span> /></span>
<span style="color:#006666 !important"></<span style="color:#4f4f4f !important">sessionState</span> ></span></code></span>
- 1
- 2
8、pages节点:用于表示对特定页设置
属性名 | 说明 |
---|---|
buffer | 是否启用了 HTTP 响应缓冲。 |
enableViewStateMac | 是否应该对页的视图状态运行计算机身份验证检查 (MAC),以放置用户篡改,默认为false,如果设置为true将会引起性能的降低。 |
validateRequest | 是否验证用户输入中有跨站点脚本攻击和SQL注入式漏洞攻击,默认为true,如果出现匹配情况就会发 HttpRequestValidationException 异常。对于包含有在线文本编辑器页面一般自行验证用户输入而将此属性设为false。 |
<span style="color:#000000"><code><span style="color:#006666 !important"><<span style="color:#4f4f4f !important">pages</span> <span style="color:#4f4f4f !important">buffer</span>=<span style="color:#009900 !important">"*"</span> <span style="color:#4f4f4f !important">enableViewStateMac</span>=<span style="color:#009900 !important">"*"</span> <span style="color:#4f4f4f !important">validateRequest</span>=<span style="color:#009900 !important">"*"</span>/></span></code></span>
- 1
9、globalization节点:用于配置应用程序的全球化设置
属性名 | 说明 |
---|---|
fileEncoding | 可选属性。设置.aspx、.asmx 和 .asax 文件的存储编码 |
requestEncoding | 可选属性。设置客户端请求的编码,默认为UTF-8 |
responseEncoding | 可选属性。设置服务器端响应的编码,默认为UTF-8 |
<span style="color:#000000"><code><span style="color:#006666 !important"><<span style="color:#4f4f4f !important">globalization</span> <span style="color:#4f4f4f !important">fileEncoding</span>=<span style="color:#009900 !important">"utf-8"</span> <span style="color:#4f4f4f !important">requestEncoding</span>=<span style="color:#009900 !important">"utf-8"</span> <span style="color:#4f4f4f !important">responseEncoding</span>=<span style="color:#009900 !important">"utf-8"</span>/></span></code></span>
- 1
关于配置文件,其他请见:https://blog.csdn.net/cxh6863/article/details/82054978
Global.asax与Web.config的比较
Web.config是对程序中应用程序的配置,效用范围在它所在目录
Global.asax是全局的,是整个网站总体的设置
Global.asax是一个可选的文件,Web.config是一个必有的文件