1. 程式人生 > >Web.config配置檔案詳解

Web.config配置檔案詳解

ASP.NET Web.config配置檔案的基本使用方法。很適合新手參看,由於Web.config在使用很靈活,可以自定義一些節點。所以這裡只介紹一些比較常用的節點。

<?xml version="1.0"?>

<!--注意: 除了手動編輯此檔案以外,您還可以使用 Web 管理工具來配置應用程式的設定。可以使用 Visual Studio 中的“網站”->“Asp.Net 配置”選項。

設定和註釋的完整列表在 machine.config.comments 中,該檔案通常位於 "Windows"Microsoft.Net"Framework"v2.x"Config 中。

-->

 <!--Webconfig檔案是一個xml檔案,configuration是xml檔案的根節點,由於xml檔案的根節點只能有一個,所以Webconfig的所有配置都是在這個節點內進行的。-->

<configuration>

  <!--指定配置節和名稱空間宣告。clear:移除對繼承的節和節組的所有引用,只允許由當前 section 和 sectionGroup 元素新增的節和節組。remove:移除對繼承的節和節組的引用。

 section:定義配置節處理程式與配置元素之間的關聯。sectionGroup:定義配置節處理程式與配置節之間的關聯。

-->

 <configSections>

    <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">

      <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35

">

        <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication"/>

      </sectionGroup>

    </sectionGroup>

    <section name="rewriter" type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter" />

 </configSections>

 <!--appSettings是應用程式設定,可以定義應用程式的全域性常量設定等資訊-->

     <appSettings>

 <add key="1" value="1" />

 <add key="gao" value="weipeng" />

 </appSettings>

 <!--連線字串設定-->

 <connectionStrings>

    <add name="ConnString" connectionString="Data Source=GAO;Initial Catalog=HBWXDate;User ID=sa;password=sa"></add>

    <add name="111" connectionString="11111" />

 </connectionStrings>

 <!--指定應用子配置設定的資源,並鎖定配置設定,以防止它們被子配置檔案重寫。page指定應用包含的配置設定的資源.allowOverride是否允許配置檔案的重寫,提高配置檔案的安全性-->

 <location path="Default.aspx" allowOverride="false">

 <!--控制asp.net執行時的行為-->

<system.web>

    <!--identity控制web應用程式的身份驗證標識.-->

    <identity impersonate="false" />

    <!--標識特定於頁的配置設定(如是否啟用會話狀態、檢視狀態,是否檢測使用者的輸入等)。<pages>可以在計算機、站點、應用程式和子目錄級別宣告.

這裡的幾個屬性的意思是預設主頁為Index,主題是Default,不檢測使用者在瀏覽器輸入的內容中是否存在潛在的危險資料(注:該項預設是檢測,如果你使用了不檢測,一要對使用者的輸入進行編碼或驗證),在從客戶端回發頁時將檢查加密的檢視狀態,以驗證檢視狀態是否已在客戶端被篡改。(注:該項預設是不驗證)禁用ViewState-->

    <pages masterPageFile="Index" theme="Default" buffer="true" enableViewStateMac="true" validateRequest="false" enableViewState="false">

      <!--controls 元素定義標記字首所在的 register 指令和名稱空間的集合-->

      <controls></controls>

      <!--將在程式集預編譯期間使用的匯入指令的集合-->

      <namespaces></namespaces>

    </pages>

    <!--預設錯誤頁設定,mode:具有On,Off,RemoteOnly 3種狀態。On表示始終顯示自定義的資訊; Off表示始終顯示詳細的asp.net錯誤資訊; RemoteOnly表示只對不在本地Web伺服器上執行的使用者顯示自定義資訊.defaultRedirect:用於出現錯誤時重定向的URL地址-->

    <customErrors defaultRedirect="Err.html" mode="RemoteOnly">

      <!--特殊程式碼編號的錯誤從定向檔案-->

      <error statusCode="403" redirect="NoAccess.htm" />

      <error statusCode="404" redirect="FileNotFound.htm" />

    </customErrors>

    <!--配置除錯和跟蹤:下面配置的意思是啟動除錯(預設),捕獲跟蹤資訊,要快取的跟蹤請求個數(15),跟蹤結果的排列順序-->

    <trace enabled="true" localOnly="false" pageOutput="true" requestLimit="15" traceMode="SortByCategory"/>

    <!-- 設定 compilation debug="true" 將除錯符號插入已編譯的頁面中。但由於這會影響效能,因此只在開發過程中將此值設定為 true。設定預設的開發語言C#。batch是否支援批處理-->

    <compilation debug="true" defaultLanguage="c#" batch="false">

      <assemblies>

        <!--加的程式集引用,每新增一個程式集,就表示你的應用程式已經依賴了一個程式集,你就可以在你的應用程式中使用了-->

        <add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>

        <add assembly="System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>

        <add assembly="System.Web.Extensions.Design, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

        <add assembly="System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>

        <add assembly="System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>

        <add assembly="System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>

        <add assembly="System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>

        <add assembly="System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>

        <add assembly="System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>

        <add assembly="System.Web.Services, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>

        <add assembly="System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>

      </assemblies>

      <!--定義用於編譯自定義資原始檔的生成提供程式的集合。-->

      <buildProviders>

        <!---->

        <add extension=".aspx" type="System.Web.Compilation.PageBuildProvider"/>

        <add extension=".ascx" type="System.Web.Compilation.UserControlBuildProvider"/>

        <add extension=".master" type="System.Web.Compilation.MasterPageBuildProvider"/>

        <add extension=".asmx" type="System.Web.Compilation.WebServiceBuildProvider"/>

        <add extension=".ashx" type="System.Web.Compilation.WebHandlerBuildProvider"/>

        <add extension=".soap" type="System.Web.Compilation.WebServiceBuildProvider"/>

        <add extension=".resx" type="System.Web.Compilation.ResXBuildProvider"/>

        <add extension=".resources" type="System.Web.Compilation.ResourcesBuildProvider"/>

        <add extension=".wsdl" type="System.Web.Compilation.WsdlBuildProvider"/>

        <add extension=".xsd" type="System.Web.Compilation.XsdBuildProvider"/>

        <add extension=".rdlc" type="Microsoft.Reporting.RdlBuildProvider, Microsoft.ReportViewer.Common, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>

      </buildProviders>

    </compilation>

      <!--通過 <authentication> 節可以配置 ASP.NET 使用的 安全身份驗證模式,以標識傳入的使用者。Windows: 使用IIS驗證方式,Forms: 使用基於窗體的驗證方式,Passport: 採用Passport cookie驗證模式,None: 不採用任何驗證方式-->

    <authentication mode="Forms">

      <!--Name: 指定完成身份驗證的Http cookie的名稱.LoginUrl: 如果未通過驗證或超時後重定向的頁面URL,一般為登入頁面,讓使用者重新登入。Protection: 指定 cookie資料的保護方式. 

可設定為:All表示加密資料,並進行有效性驗證兩種方式,None表示不保護Cookie,Encryption表示對Cookie內容進行加密,validation表示對Cookie內容進行有效性驗證,TimeOut: 指定Cookie的失效時間. 超時後要重新登入。-->

      <forms name=".ASPXUSERDEMO" loginUrl="Login.aspx" protection="All" timeout="30"/>

    </authentication>

    <!--控制對 URL 資源的客戶端訪問(如允許匿名使用者訪問)。此元素可以在任何級別(計算機、站點、應用程式、子目錄或頁)上宣告。必需與<authentication> 節配合使用。此處的意思是對匿名使用者不進行身份驗證。拒絕使用者weipeng-->

    <authorization>

      <allow users="*"/>

      <deny users="weipeng"/>

      <allow users="aa" roles="aa" />

    </authorization>

    <!--站點全球化設定,requestEncoding: 它用來檢查每一個發來請求的編碼.responseEncoding: 用於檢查發回的響應內容編碼.fileEncoding:用於檢查aspx,asax等檔案解析的預設編碼,預設的編碼是utf-8-->

    <globalization requestEncoding="gb2312" responseEncoding="gb2312" fileEncoding="gb2312" />

    <!--會話狀態設定。mode: 分為off,Inproc,StateServer,SqlServer幾種狀態 mode = InProc 儲存在程序中特點:具有最佳的效能,速度最快,但不能跨多臺伺服器儲存共享.mode = "StateServer" 儲存在狀態伺服器中特點:當需要跨伺服器維護使用者會話資訊時,使用此方法。但是資訊儲存在狀態伺服器上,一旦狀態伺服器出現故障,資訊將丟失. mode="SqlServer" 儲存在sql server中特點:工作負載會變大,但資訊不會丟失

    stateConnectionString :指定asp.net應用程式儲存遠端會話狀態的伺服器名,預設為本機。sqlConnectionString:當用會話狀態資料庫時,在這裡設定連線字串。Cookieless:設定為flase時,表示使用cookie會話狀態來標識客戶.timeout表示會話超時時間。-->

    <sessionState mode="InProc" stateConnectionString="tcpip=127.0.0.1:42424" sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes" cookieless="false" timeout="20"></sessionState>

    <!--為 ASP.NET 應用程式配置頁的檢視狀態設定。設定要儲存在頁歷史記錄中的項數。-->

    <sessionPageState historySize="9"/>

    <!--配置asp.net http執行庫的設定。可以在計算機,站點,應用程式和子目錄級別宣告

允許最多的請求個數100,最長允許執行請求時間為80秒,控制使用者上傳檔案的大小,預設是4M。useFullyQualifiedRedirectUrl客戶端重定向不需要被自動轉換為完全限定格式。-->

    <httpRuntime appRequestQueueLimit="100" executionTimeout="80" maxRequestLength="40960" useFullyQualifiedRedirectUrl="false"/>

    <!--httpModules在一個應用程式內配置 HTTP 模組。-->

    <httpModules>

      <add name="OutputCache" type="System.Web.Caching.OutputCacheModule" />

      <add name="Session" type="System.Web.SessionState.SessionStateModule" />

      <add name="WindowsAuthentication" type="System.Web.Security.WindowsAuthenticationModule" />

      <add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule" />

      <add name="PassportAuthentication" type="System.Web.Security.PassportAuthenticationModule" />

      <add name="RoleManager" type="System.Web.Security.RoleManagerModule" />

      <add name="UrlAuthorization" type="System.Web.Security.UrlAuthorizationModule" />

      <add name="FileAuthorization" type="System.Web.Security.FileAuthorizationModule" />

      <add name="AnonymousIdentification" type="System.Web.Security.AnonymousIdentificationModule" />

      <!--自定義的URL重寫,type基本上就是dll名-->

      <add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter" />

      <add name="Profile" type="System.Web.Profile.ProfileModule" />

    </httpModules> 

    <!--httpHandlers用於根據使用者請求的URL和HTTP謂詞將使用者的請求交給相應的處理程式。可以在配置級別的任何層次配置此節點,也就是說可以針對某個特定目錄下指定的特殊檔案進行特殊處理。

    add:指定對映到處理程式的謂詞/路徑。clear:移除當前已配置或已繼承的所有處理程式對映。remove:移除對映到處理程式的謂詞/路徑。remove 指令必須與前一個 add 指令的謂詞/路徑組合完全匹配。該指令不支援萬用字元。-->

    <httpHandlers>

      <remove verb="*" path="*.asmx"/>

      <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>

      <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>

      <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>

      <