[Turn] Configure the sub-directory Web.config to eliminate inheritance and create multiple websites with virtual directories

[Turn] Configure the sub-directory Web.config to eliminate inheritance and create multiple websites with virtual directories

Original link: http://www.wtnzone.com/post/2011/02/20/Set-Web-Config-to-Turn-Inheritance-Off.aspx

ASP.NET provides a powerful Web.config to configure a website. Generally speaking, a website has only one Web.config file in the root directory. Sometimes we want the subdirectories to have different permissions or parameter settings, then you can place them in the corresponding subdirectories. Add a Web.config configuration file and add our new configuration parameters. It should be noted here that the subdirectory web.config inherits all the settings of the parent directory. Therefore, if the subdirectory contains a website that is different from the parent directory, the configuration of the parent directory cannot be shared, otherwise it is very likely Will conflict. For example, many hosts provide a way to bind domain names to subdirectories to be able to create multiple websites. Take Godaddy's Windows host as an example. If we want to create a brand new website through subdirectories, if we don't configure the correct Web. config, it is likely to report HTTP 500 Error Internal Server Error. The reason for this error will be analyzed below and the correct configuration method will be given.

View HTTP 500 error details

It's easy to see errors on your own machine, but after uploading to a remote host, it often only prompts friendly error messages, so we can't know the details. In order to view the cause of the error, you need to display the error details, modify the Web.config file of the root directory and subdirectory website, and add the following configuration in the system.webServer node,

<httpErrors errorMode="Detailed"/>

Add the following configuration to the system.web node

<customErrors mode="Off"/>

So you can see the detailed source of the error. The 500 Internal Server Error error caused by the subdirectory website is mostly caused by conflicts with the Web.config configuration. For example, a connectioinString node with the name "connStr" is defined in the root directory. If a connectionString node with the same name as "connStr" is defined in the subdirectory, an error will occur, because any configuration node does not allow duplicate keys to be added , otherwise it will Prompt errors such as Dulplicate key. So how to avoid configuration conflicts between subdirectories and root directories? There are generally two solutions. One is to modify the Web.config in the root directory to eliminate the inheritance relationship between the subdirectory and the root directory, that is, the configuration of the subdirectory Web.config is not affected by the root directory Web.config; The second method is to modify the Web.config of the subdirectory, and clear or remove the configuration information of the root directory before adding all configurations that may conflict with the Web.config of the root directory. The two methods of making the subdirectory Web.config independent of the root directory are discussed in detail below.

Subdirectories use independent Web.config configuration method

First look at the first method , which is to eliminate the inheritance relationship by modifying the Web.config of the root directory. Add a layer of location outside the system.web of the root directory Web.config file or all configuration nodes that do not want subdirectories to inherit as follows:

<configuration>
  <!--...Other configuration...-->
  <location path="." inheritInChildApplications="false">
    <system.web>

    </system.web>
  </location>
  <!--...Other configuration...-->
</configuration>

Here inheritInChildApplications is well understood, that is, whether to allow sub-directory inheritance, the default is true, we can modify it to false to avoid inheritance. The advantage of this method is that it is very simple, but not flexible enough. At the same time, it may still report errors in the face of more complicated Web.config configurations. For example, the root directory and subdirectories have different system.webServer configurations. According to this method, the location restriction is added outside the system.webServer of the root directory. Unfortunately, a 500 error will occur under IIS7. Of course, there are also solutions, you can refer to the following article.

" SOLVED: IIS7, validateIntegratedModeConfiguration and inheritInChildApplications clash "

Then look at the second method , instead of modifying the Web.config file in the root directory, but modifying the Web.config in the subdirectory. Assuming that the Web.config of the root directory sets a connection string named BlogEngine, to use another connection string named BlogEngine in the subdirectory, you need to clear the existing connection string (the connectionString inherited from the root directory) Settings), clear all the configurations, you can use the clear syntax, clear the configuration of the specified name, you can use the remove syntax, as follows

<--Web.config of the root directory-->
<connectionStrings>
  <add name="BlogEngine" connectionString="Data Source=localhost\SQLEXPRESS; Initial Catalog=BlogEngine1; User ID=xxx; Password=xxx" providerName="System.Data.SqlClient"/>
</connectionStrings>
<--Web.config of subdirectory (clear method)-->
<connectionStrings>
  <clear/>
  <add name="BlogEngine" connectionString="Data Source=localhost\SQLEXPRESS; Initial Catalog=BlogEngine2; User ID=xxx; Password=xxx" providerName="System.Data.SqlClient"/>
</connectionStrings>
<--Web.config of subdirectory (remove method)-->
<connectionStrings>
  <remove name="BlogEngine"/>
  <add name="BlogEngine" connectionString="Data Source=localhost\SQLEXPRESS; Initial Catalog=BlogEngine2; User ID=xxx; Password=xxx" providerName="System.Data.SqlClient"/>
</connectionStrings>

Here is just using connectionString as an example. It can be applied to all configurable nodes when used. Any configuration node can use clear and remove nodes to clear the inherited configuration first, and then add a new configuration. This method is more flexible, and at the same time it can retain part of the common configuration in the root directory Web.config (without having to reset all of them). The following is a more complicated example, which is the configuration of the root directory and subdirectories on system.webServer.

<--Web.config system.webServer configuration node in the root directory-->
<modules>
  <remove name="ScriptModule"/>
  <add name="WwwSubDomainModule" type="BlogEngine.Core.Web.HttpModules.WwwSubDomainModule, BlogEngine.Core"/>
  <add name="UrlRewrite" type="BlogEngine.Core.Web.HttpModules.UrlRewrite, BlogEngine.Core"/>
  <add name="CompressionModule" type="BlogEngine.Core.Web.HttpModules.CompressionModule, BlogEngine.Core"/>
  <add name="ReferrerModule" type="BlogEngine.Core.Web.HttpModules.ReferrerModule, BlogEngine.Core"/>
  <add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</modules>
<--Web.config system.webServer configuration node in subdirectory-->
<modules runAllManagedModulesForAllRequests="true">
  <remove name="WwwSubDomainModule"/>
  <remove name="UrlRewrite"/>
  <remove name="CompressionModule"/>
  <remove name="ReferrerModule"/>
  <remove name="ScriptModule"/>
  <add name="UrlRewrite" type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter"/>
  <add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</modules>
<handlers>

As you can see, the subdirectory has removed all the Modules defined by the root directory (WwwSubDomainModule, UrlRewrite, CompressionModule, ReferrerModule, ScriptModule), and added its own UrlRewrite and ScriptModule modules. This configuration not only eliminates conflicts, but also achieves partial inheritance of the configuration (only part of the subdirectory is different from the root directory), but the first method cannot achieve partial inheritance.

summary

Knowing the inheritance method of Web.config and the configuration method of subdirectories, we can easily use subdirectories to build a new website independent of the parent directory, using completely different databases and connection strings, completely different Modules, and completely different The HttpHandler is really cool. Take Godaddy as an example, bind the domain name to Godaddy's subdirectory, modify the Web.config of the subdirectory so that it does not conflict with the configuration of the root directory, and a new website will be launched! It's that simple.

Reference: https://cloud.tencent.com/developer/article/1513598 [Transfer] Configure the subdirectory Web.config to eliminate inheritance and create multiple websites with virtual directories-Cloud + Community-Tencent Cloud