Try   HackMD

筆記_Web.config是什麼??(大致了解)


tags: 基礎知識 專有名詞 .NET

相關連結
web.config百度百科

Web應用程式開發-ASP.NET Web.config

Web.config的理解

[IIS] ASP.NET Web.Config 標籤基本介紹

淺談web.config

The ASP.NET Web.config File Demystified Article

總論

  1. Web.config是一份XML格式設定文件

A(段落而已)

Web.config是一份XML格式設定文件
用來記錄ASP.Net中的Web應用程式配置

Web.config是開發Asp.net的網站或應用程式時,對整個網站或應用程式的設定檔,這個檔案在client端是無法看到的,也因為安全性的關係,透過瀏覽器也是無法看到的

B

IIS啟動時會自動加載該文件項目
(Asp.net網站IIS啟動時會載入配置檔案中的配置資訊,然後快取這些資訊,這樣就不必每次去讀取配置資訊。)
一些網站的設定也不需要每次都去修改IIS配置

C

在每次建立新的web應用程序時
Web.config文件是基本配置
Visual Studio .NET會在跟目錄下默認生成此XML文件
包含默認的設定配置
所有子目路都繼承此Web.config配置
如果要修改某一子目錄配置,可在該子目錄下建立一個Web.config文件,可以繼承父文件配置外也可修改或重寫富目錄定義之配置
(就繼承的概念)

Web.config有階層關係

D(節點配置)

<出自Web.config的理解>
Web.config是一份XML格式設定文件
其根root節點為<configuration>
在根節點下<configuration>常見的子節點有

  1. <appSettings>
  2. <connectionStrings>
  3. <compilation>
  4. <authentication>
  5. <httpHandlers>
  6. <httpRuntime>
  7. <pages>
  8. <sessionState>
  9. <globalization>
<system.web> <authentication> <!-- 設定認證模式 --> <authorization> <!-- 授權角色使用者 --> <browserCaps> <!-- 瀏覽器相容性相關 --> <clientTarget> <!-- 管理用戶端瀏覽器集合 --> <compilation> <!-- 彙整與編譯設定 --> <customErrors> <!-- 客製化錯誤頁面的設定 --> <globalization> <!-- 語系跟編碼規則--> <httpHandlers> <!-- HttpMethod與Handler的設定 --> <httpModules> <!-- Http模組設定 --> <httpRuntime> <!-- ASP.NET執行環境的設定 --> <identity> <!-- 控制執行應用程式的身分 --> <machineKey> <!-- 表單驗證與Session加解密的方式 --> <pages> <!-- 設定頁面資訊 --> <processModel> <!-- 應用程式的Process設定 --> <securityPolicy> <!-- 定義檔案安全層級 --> <sessionState> <!-- 設定Session狀態 --> <trace> <!-- 追蹤資訊 --> <trust> <!-- 設定該應用程式信任層級 --> <webServices> <!-- web services xml 定義 --> </system.web>

D


一個ASP.NET 應用程式對應到一個web.config (程式跟目錄與web.config同資料夾),web.config 可視為該應用程式的專屬設定檔。

web.config 設定會覆寫 machine.config (path : C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config [依據版本會有所不同])

web.config 也有階層覆蓋的概念(如MVC的View中的web.config會覆寫跟目錄下的web.config)


web.config 階層:(承D段)

configuration 標籤: 套用至目前的目錄與所有子目錄的設定(根元素)

<configuration>

  <configSections> </configSections>
  <appSettings> </appSettings> 
  <connectionStrings> </connectionStrings>
  <system.web> </system.web>

</configuration>

configSections 標籤: 標示程式碼與設定值的區段名稱 (該標籤需要擺在第一個)

<configuration>

   <configSections>
      <section name="sampleSection" type="System.Configuration.SingleTagSectionHandler" />
   </configSections>

   <sampleSection setting1="Value1"/>

   <appSettings> <!-- machine.config內configSections已宣告appSettings這個section-->
      <add name="" value="">
   <appSettings/>

   <connectionStrings><!-- machine.config內configSections已宣告connectionStrings這個section-->
      <add name="Sales"  providerName=""  connectionString= "" />
   </connectionStrings>
</configuration>

system.web 標籤: ASP.NET Web組態設定區段的根目錄元素

<system.web> <authentication> <!-- 設定認證模式 --> <authorization> <!-- 授權角色使用者 --> <browserCaps> <!-- 瀏覽器相容性相關 --> <clientTarget> <!-- 管理用戶端瀏覽器集合 --> <compilation> <!-- 彙整與編譯設定 --> <customErrors> <!-- 客製化錯誤頁面的設定 --> <globalization> <!-- 語系跟編碼規則--> <httpHandlers> <!-- HttpMethod與Handler的設定 --> <httpModules> <!-- Http模組設定 --> <httpRuntime> <!-- ASP.NET執行環境的設定 --> <identity> <!-- 控制執行應用程式的身分 --> <machineKey> <!-- 表單驗證與Session加解密的方式 --> <pages> <!-- 設定頁面資訊 --> <processModel> <!-- 應用程式的Process設定 --> <securityPolicy> <!-- 定義檔案安全層級 --> <sessionState> <!-- 設定Session狀態 --> <trace> <!-- 追蹤資訊 --> <trust> <!-- 設定該應用程式信任層級 --> <webServices> <!-- web services xml 定義 --> </system.web>

location 標籤: 使用該標籤包裝起來,可設定該內容對指定的子資料夾有效。

<configuration>
   <system.web>  
   </system.web>
   <location path="sub1"><!-- Configuration for the "Sub1" subdirectory. -->
      <system.web>
      </system.web>
   </location>
</configuration>