首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> ASP.NET 基礎 >> 關於ASP.NET

ASP.NET 2.0 程式化存取 Web.config 設定:SystemWebSectionGroup 完整應用指南

     編輯:關於ASP.NET

ASP.NET 2.0 引入 SystemWebSectionGroup 類別,提供強型別、物件導向的方式程式化存取 web.config 中 <system.web> 區段群組的所有設定區段,免去手動剖析 XML 的麻煩。

核心用法範例

SystemWebSectionGroup ws = new SystemWebSectionGroup();

// 判斷 debug 模式
CompilationSection cp = ws.Compilation;
bool isDebug = cp.Debug;  // 對應 compilation/@debug

// 判斷驗證模式
AuthenticationSection auth = ws.Authentication;
AuthenticationMode mode = auth.Mode;  // Forms, None, Passport, Windows

AuthenticationMode 列舉值對照

成員說明
FormsASP.NET 表單驗證
None無驗證
PassportMicrosoft Passport(已淘汰)
WindowsWindows 整合驗證(Basic、Digest、NTLM/Kerberos、憑證)

SystemWebSectionGroup 主要公開屬性對照表

屬性名稱對應設定區段型別
AnonymousIdentification<anonymousIdentification>AnonymousIdentificationSection
Authentication<authentication>AuthenticationSection
Authorization<authorization>AuthorizationSection
BrowserCaps<browserCaps>HttpCapabilitiesSectionHandler
ClientTarget<clientTarget>ClientTargetSection
Compilation<compilation>CompilationSection
CustomErrors<customErrors>CustomErrorsSection
Deployment<deployment>DeploymentSection
DeviceFilters<deviceFilters>DeviceFiltersSection
Globalization<globalization>GlobalizationSection
HealthMonitoring<healthMonitoring>HealthMonitoringSection
HostingEnvironment<hostingEnvironment>HostingEnvironmentSection
HttpCookies<httpCookies>HttpCookiesSection
HttpHandlers<httpHandlers>HttpHandlersSection
HttpModules<httpModules>HttpModulesSection
HttpRuntime<httpRuntime>HttpRuntimeSection
Identity<identity>IdentitySection
MachineKey<machineKey>MachineKeySection
Membership<membership>MembershipSection
OutputCache<outputCache>OutputCacheSection
Pages<pages>PagesSection
Profile<profile>ProfileSection
RoleManager<roleManager>RoleManagerSection
SessionPageState<sessionPageState>SessionPageStateSection
SessionState<sessionState>SessionStateSection
SiteMap<siteMap>SiteMapSection
Trace<trace>TraceSection
Trust<trust>TrustSection
UrlMappings<urlMappings>UrlMappingsSection
WebControls<webControls>WebControlsSection
WebParts<webParts>WebPartsSection
XhtmlConformance<xhtmlConformance>XhtmlConformanceSection

進階應用場景

  • 執行期動態調整:依環境切換 customErrors 模式、調整 sessionState 超時。
  • 多租戶設定隔離:程式化讀取租戶專屬設定區段。
  • 健康監控整合:檢查 healthMonitoring 規則動態啟用/停用事件記錄。
  • 部署驗證工具:啟動時自動驗證關鍵設定(如 machineKey、trust level)符合安全基線。

注意事項

  • SystemWebSectionGroup 僅存取 <system.web>,<connectionStrings>、<appSettings>、<system.webServer> 等需使用 WebConfigurationManager。
  • 修改區段屬性需確保執行身分擁有寫入 web.config 權限,且會觸發應用程式重啟。
  • 唯讀屬性(如 Compilation.Debug)僅供讀取,寫入需透過 Configuration 物件 Save()。

善用 SystemWebSectionGroup 讓設定存取型別安全、程式碼清晰、維護容易。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved