= programmatically change web.config
Dnes jsem narazil na nutnost programové změny položek v konfiguračním souboru web.config.
Příklad ukáži na změně v sekci customError. Ukázkový konfigurační soubor je:
1: <authenticationmode="Windows" />
2: <!--
3: The <customErrors> section enables configuration
4: of what to do if/when an unhandled error occurs
5: during the execution of a request. Specifically,
6: it enables developers to configure html error pages
7: to be displayed in place of a error stack trace. -->
8:
9: <customErrorsmode="RemoteOnly"
10: defaultRedirect="GenericErrorPage.htm">
11: <errorstatusCode="403"redirect="NoAccess.htm" />
12: <errorstatusCode="404"redirect="FileNotFound.htm" />
13: </customErrors>
14:
15: </system.web>
Programově lze poté hodnoty této sekce změnit následujícím způsobem:
1: // nacteni sekce
2: Configuration configuration =
3: WebConfigurationManager.OpenWebConfiguration("~");
4: CustomErrorsSection section =
5: (CustomErrorsSection)configuration.GetSection("system.web/customErrors");
6:
7: // zmena hodnoty polozky sekce
8: section.DefaultRedirect = "SomePage.htm";
9:
10: //ulozeni konfigurace
11: configuration.Save();