Web.config file consists of vital information for your website to work including authentication, server, and application specific settings. Depending on your website’s requirements, this file may grow quickly to a limit that IIS refuses to read to prevent possible vulnerability attacks.
Maximum size for web.config file
The default maximum limit for the web.config file is 250 KB. However, you can change this value by editing the registry key below (Reference). Make sure to restart the server after this change.
(REG_DWORD) type. More information about this key is here.
If 32-bit is enabled for your application pool, make sure to edit the following key instead:
If you exceed the limit, IIS will display this error message: “Cannot read configuration file because it exceeds the maximum file size“
Is there a better way to manage big web.config file?
Yes. Instead of growing the file size, you can separate the configuration into multiple files. Let’s look at two scenarios.
Extensive amount of URL Rewrite rules
If you have hundreds of URL Rewrite rules (This is not an unusual scenario), you can create rewrite maps and store them in a separate file.
Let’s say you have a rewrite map like the one below. Save it in a file called rewritemaps.config.
<rewriteMaps> <rewriteMap name="Redirects"> <add key="/fromthisURL" value="/tothisURL" /> <add key="/fromthisURL2" value="/tothisURL2" /> </rewriteMap> </rewriteMaps>
Then refer this file from your web.config file:
<configuration> <system.webServer> <rewrite> <rewriteMaps configSource="rewritemaps.config"><rewriteMaps> <rule name="Redirect rule"> ... </rule> </rewrite> </system.webServer> </configuration>
Just like rewrite maps, you can store rewrite rules themselves in a separate file as well:
<rules configSource="rewriteRules.config" />
Please note that just like web.config file, rewritemaps.config and rewriteRules.config files have 250 KB size limit as well.
You may ask if it’s a good idea to have hundreds of URL rewrite rules or maps in config files. For performance, redirection by using URL Rewrite rules is better as the requests will be redirected before they are handled by ASP.NET handler. For maintenance overhead, it’s better to do redirection and manage URLs in database so config files in the server won’t need to be edited often.
It’s easy to cause syntax errors while adding or editing too many rewrite rules. Here is one of the common syntax issues with a solution: The configuration section rewrite/globalRules cannot be read because it is missing a section declaration
Extensive amount of application settings
If you have too many application specific settings (or any other type of settings), you can use multiple web.config files.
In the example below, application settings and connecting strings are stored in separate config files (Reference). They are referenced from the main web.config:
<appSettings configSource="app.config"> </appSettings> <connectionStrings configSource="database.config"> </connectionStrings>
Maximum count for web.config files
Based on my research, there is no theoretical limit. However, CPU and memory usage can play a factor when there are too many config files mapped.
You may end up having too many web.config files in root directory and subfolder. If you want IIS to ignore the certain config files, check this post out: Configure IIS to ignore web.config files in application subfolders