认识Web.config文件

本文详细介绍了ASP.NET Web应用程序的核心配置文件Web.config。包括其基本结构、关键元素及其功能,如system.web、appSettings 和 connectionStrings等Section Group的作用和配置示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

一、 認識Web.config文件

Web.config文件是一个XML文本文件,用來儲存 ASP.NET Web應用程序的配置信息。當你在.NET新建一個Web應用程序后,默認情況下會在根目錄下自動生成一個Web.config文件with default configuration setting。如果你想修改子目錄的配置屬性,你可以在該子目錄下新建一個Web.config文件。它可以提供除從父目录繼承的配置信息外,還可以重寫或修改父目錄中定義的設置。

 

 

二、 Web.config文件詳解

Web.config 檔至少必須具有 <configuration> 元素及 <system.web> 元素,configuration是每個配置文件中均需要的根元素,所有配置信息 resides between the <configuration> and </configuration> root XML tags.

一個最簡單的xml文件如下所示:

<? xml version="1.0" encoding="utf-8"  ?>
< configuration >
  
< system.web >

  
</ system.web >
</ configuration >

Web.config 檔的第一行會描述此文件為 XML 格式,並指定字元編碼類型。所有 .config 檔的第一行都必須相同。

底下幾行則會標示 Web.config 檔之 <configuration> 元素及 <system.web> 元素的開頭與結尾。但光靠這兩個元素將不會有任何作用。不過,這幾行會提供能讓您在未來新增sections group的架構。大部分的 ASP.NET 組態設定都是加在 <system.web> 及 </system.web> 這兩行之間。

 

除了上述兩個元素外,你可以增加你需要的sections group 進來,如加入appSettings元素:

<configuration> 
 
<system.web> 
   
<!— sections-->    
 
</system.web> 
 
<appSettings> 
   
<!— sections -->    
 
</appSettings > 
</configuration>

 

下面將簡單介紹幾個常用的Section Group的group以加深了解。

 

1. The system.web Section Group

指定配置文件中 ASP.NET 配置設置根元素,并包含用于配置 ASP.NET Web 應用程序和控制程序行為方式的配置元素,它的結構如下:

ContractedBlock.gif ExpandedBlockStart.gif Code
<system.web>
   
<authentication>
   
<authorization>
   
<browserCaps>
   
<clientTarget>
   
<compilation>
   
<customErrors>
   
<globalization>
   
<httpHandlers>
   
<httpModules>
   
<httpRuntime>
   
<identity>
   
<machineKey>
   
<pages>
   
<processModel>
   
<securityPolicy>
   
<serviceDescriptionFormatExtensionTypes>
   
<sessionState>
   
<trace>
   
<trust>
   
<webServices>
</system.web>

 

<authentication>

配置 WEB應用程序身份驗證支持(Windows、Forms、PassPort、None四種)。<authentication> 元素需与<authorization> 配合使用。You'll enter the value "None" if anyone may access your application. If authentication is required, you'll use "Windows", "Forms" or "Passport" to define the type of authentication. For example:

  < authentication  mode ="Windows"   />

 

<authorization>

使用<allow> or <deny> 子標簽控制對 URL資源的客戶端訪問(如允許匿名訪問)。此元素可以在任何級別(計算機、站點、應用程序、子目錄或頁)上聲明。需與<authentication> 配合使用。

    < authorization >  
       
< allow  roles ="Administrators,Users"   />  
       
< deny  users ="*"   />  
   
</ authorization >

注記:在這個例子里,擁有Administrators 或者Users權限的用戶將被允許進入,而其他用戶則被禁止訪問。  

 

<compilation>

這里,你能夠configure the compiler settings for ASP.NET. 較常用的有debug和defaultLanguage. 默認的debug屬性為“True”, 它能讓瀏覽器顯示編譯信息.在程序編譯完成交付使用之后應將其設為“False”。  defaultLanguage 屬性告訴ASP.NET現在用的是什么語言進行編譯,如“c#”或“vb”.

 

<customErrors>

為應用程序提供有關自定義錯誤信息的信息。它不适用于 XML Web services 中發生的錯誤。
示例:當錯誤發生時,根據error number將網頁跳轉到自定義的error page。

    < customErrors  mode ="RemoteOnly"  defaultRedirect ="/error.html" >  
       
< error  statusCode ="403"  redirect ="/accessdenied.html"   />  
       
< error  statusCode ="404"  redirect ="/pagenotfound.html"   />  
   
</ customErrors >

 

<globalization>

配置應用程序的globalization setting, 說簡單點就是設置編碼 ,如指定請求和響應的編碼,還有設定區域culture。

   < globalization  requestEncoding ="utf-8"  responseEncoding ="utf-8"  culture ="nl-NL"   />

 

<httpRuntime>

配置 ASP.NET HTTP運行庫設置。
示例:控制用户上傳文件最大為4M,最長時間為60秒,最多請求數為100。

< httpRuntime  maxRequestLength ="4096"  executionTimeout ="60"  appRequestQueueLimit ="100" />

 

<sessionState>

為當前應用程序配置會話狀態(如設置是否啟用會話狀態,會話狀態保存位置).詳情可參看:sessionstate元素

< sessionState  mode ="InProc"  cookieless ="true"  timeout ="20" />

 

<trace>

配置 ASP.NET 跟蹤服務,主要用來測試判斷程序哪里出錯。

<trace enabled="true" localOnly="true" pageOutput="false" />

 

2. The AppSettings Section Group

      appSettings 元素存儲自定義應用程序配置信息,如文件路徑、XML Web services URL 或存儲在應用程序的 .ini 文件中的任何信息。可以使用 ConfigurationSettings 類在代碼中訪問 appSettings 元素中指定的鍵/值對(key/value)。

      您可以使用 file 屬性指定一個配置文件,該配置文件提供其他設置或重寫 appSettings 元素中指定的設置。您可以對 file屬性用于源代碼管理組開發方案,例如,當用戶需要重寫在應用程序配置文件中指定的設置時。在 file 屬性中指定的配置文件必須將 appSettings 元素(而不是 configuration 元素)作為根結點。

      在 .NET Framework 2.0 版應用程序中,將數據庫連接字符串存儲在 connectionStrings 元素(ASP.NET 設置架构)集合中,而不是存儲在應用程序設置集合中。

 

    下面的代碼示例演示如何在配置文件中定義自定義應用程序設置。

    < appSettings >  
       
< add  key ="sqlConn"  value ="Server=myPc;Database=Northwind"   />  
       
< add  key ="smtpServer"  value ="smtp.mydomain.com"   />  
   
</ appSettings >

      在程序中,如果要取得sqlConn的值,只需要如下的一句代碼

 ConfigurationSettings.AppSettings( " sqlConn " )

 

 

 3. The connectionStrings Section Group

为 ASP.NET 應用程序和 ASP.NET 功能指定數據庫連接字符串(key/value的形式)的集合。

此元素是 .NET Framework 2.0 版中的新元素,它的結構如下: 

< connectionStrings  >  
   
< add  />          <!-- 向连接字符串集合添加名称/值对形式的连接字符串。  -->
   
< clear  />        <!-- 移除所有对继承的连接字符串的引用,仅允许那些由当前的 add 元素添加的连接字符串。  -->
   
< remove  />       <!-- 从连接字符串集合中移除对继承的连接字符串的引用。  -->
</ connectionStrings >

 

下面的代碼示例演示如何配置兩個連接字符串。

< configuration >
<!--  Other configuration settings  -->

< connectionStrings >

  
< add  name ="Sales"  
       providerName
="System.Data.SqlClient"
       connectionString
= "server=myserver;database=Products;uid=salesUser;pwd=sellMoreProducts"   />

  
< add  name ="NorthWind"  
       providerName
="System.Data.SqlClient"  
       connectionString
="server=.;database=NorthWind;Integrated Security=SSPI"   />

</ connectionStrings >

</ configuration >

 

 

Reference:

http://www.sitepoint.com/article/web-config-file-demystified/ 
http://msdn.microsoft.com/zh-cn/library/ms228147(VS.80).aspx
http://dev.youkuaiyun.com/article/82/82544.shtm

转载于:https://www.cnblogs.com/iswszheng/archive/2009/05/26/1486447.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值