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
| 成員 | 說明 |
|---|---|
Forms | ASP.NET 表單驗證 |
None | 無驗證 |
Passport | Microsoft Passport(已淘汰) |
Windows | Windows 整合驗證(Basic、Digest、NTLM/Kerberos、憑證) |
| 屬性名稱 | 對應設定區段 | 型別 |
|---|---|---|
| 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 讓設定存取型別安全、程式碼清晰、維護容易。