Is it possible to have separate config files for specific sections of the web.config? Specifically I'd like to move IIS 7's rewrite section out of the web.config and into it's own config file.
Asked
Active
Viewed 2.3k times
31
-
1If you tried move **runtime section**: The **runtime section** happily accepts the configSource attribute but it ***does not read the external file*** – Kiquenet Jun 30 '15 at 12:34
2 Answers
44
You can certainly move your rewrite rules and mappings out to a separate file:
<system.webServer>
<rewrite>
<rewriteMaps configSource="rewritemaps.config" />
<rules configSource="rewriteRules.config" />
</rewrite>
</system.webServer>
In addition you can move quite a few configuration sections to their own files:
<appSettings configSource="appSettings.config" />
[Docs]
<connectionStrings configSource="connectionStrings.config"/>
[Docs]
<pages configSource="pages.config"/>
[Docs]
For more info see this page which will help you decide if a configuration section can be stored externally:
-
We got a problem with appSettings and MVC project. After moving appSettings section to a seperate file, VS could not open .cshtml files anymore. It shows a "The operation could not be completed" error. Trying to figure this out, I realzed the problem is due to
settings. I also tried to use "file" attribute instead of "configSource" in order to keep "webpages:version" on web.config, but it didnt worked. Any thoughts? – Luty Feb 13 '15 at 14:11 -
2The **runtime section** happily accepts the configSource attribute but it ***does not read the external file*** – Kiquenet Jun 30 '15 at 12:27
-
@Luty any solution about it ? appSettings not problem using **configSource**. The problem was another issue. – Kiquenet Jun 30 '15 at 12:39
-
@Kiquenet Sorry about late response. I kept appSettings in main config file to search for a solution later, but the project where I facing this problem was discontinued. – Luty Sep 08 '15 at 20:17
-
New documentation location: https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/connection-strings-and-configuration-files#using-external-configuration-files – Der_Meister Mar 15 '18 at 08:09
-
I tried doing a seperate config file in a parent folder and the fragility of iis was heavily exposed, an incoherent error message, wasted an hour trying to figure out what is going on and finally copying the file back within the folder and its working. that reminded me why I use kestrel and docker containers whenever I can. IIS needs a rewrite or a retirement, its no longer fit for purpose – PBo Sep 03 '20 at 21:08
-
-
Its a permission issue, but the whole iis setup seems out of date and out of touch. I did a similar thing with .net core appsettings, and it worked first time, without even testing it. Engineers love technical reasoning overload (I fall into that trap myself at times), but time is expensive and the effort required to get the bottom of incoherent error message is not worth while, I need clear reasons and not puzzles to spend endless time figuring out. Microsoft need to retire or invest in improving some of the legacy applications that come with windows. There are plent open source alternatives. – PBo Sep 21 '20 at 23:38
-
@PBo Well, you need to spend time learning Windows security and IIS security (application pool identities). Don't blame the platform for something you or your employer are not willing to spend time to understand. I also fail to see the benefit of storing these config files in a "parent" directory, by which I guess you mean outside of the site root? – Kev Sep 22 '20 at 00:49
-
The employer wants you to use the best tools taking the least amount of time (which also has a monetary cost to the employer). I was able to achieve this quickly using kestrel server with no issues, no point wasting time on details that are not relevant. the issue isnt whether I know security or not. the error thrown did not point to anything specific and no useful information to help solve the problem. If the error said file permissions, cannot use parent directory, it would have been resolved quickly. But throwing some cryptic error is not a wise option. – PBo Sep 23 '20 at 14:40
-
Storing settings outside the directory, simplifies deployment and prevents accidental commits of sensitive settings such as api keys or connection strings. – PBo Sep 23 '20 at 14:42
21
Yes, this is possible.
For each configuration section, you can set the configSource
attribute to the location (file) where you are holding the configuration.
E.g.:
<appSettings configSource="appSettings.config" />
And in appSettings.config:
<?xml version="1.0" encoding="UTF-8"?>
<appSettings>
<add key="myKey" value="myValue" />
</appSettings>

Matthew Abbott
- 60,571
- 9
- 104
- 129

Oded
- 489,969
- 99
- 883
- 1,009