Chinaunix首页 | 论坛 | 博客
  • 博客访问: 32792
  • 博文数量: 27
  • 博客积分: 1080
  • 博客等级: 少尉
  • 技术积分: 220
  • 用 户 组: 普通用户
  • 注册时间: 2009-05-09 10:29
文章分类

全部博文(27)

文章存档

2011年(1)

2010年(15)

2009年(11)

我的朋友

分类: 系统运维

2009-08-15 14:40:11

一、 認識Web.config文件

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

 

 

二、 Web.config文件詳解

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

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

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

  
system.web>
configuration>

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

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

 

除了上述兩個元素外,你可以增加你需要的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 應用程序和控制程序行為方式的配置元素,它的結構如下:


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

 

配置 WEB應用程序身份驗證支持(Windows、Forms、PassPort、None四種)。 元素需与 配合使用。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" />

 

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

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

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

 

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

 

 

為應用程序提供有關自定義錯誤信息的信息。它不适用于 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 setting, 說簡單點就是設置編碼 ,如指定請求和響應的編碼,還有設定區域culture。

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

 

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

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

 

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

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

 

配置 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 />      
   
<remove />     
connectionStrings>

 

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

<configuration>


<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://dev.csdn.net/article/82/82544.shtm

阅读(515) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~