Configure a subdirectory of Web. config to eliminate inheritance and create multiple Web sites with virtual directories

Source: Internet
Author: User
Tags blogengine connectionstrings

Asp. NET provides a powerful Web. config to configure the site, in general, a Web site has only one root directory of the Web. config file, sometimes we want subdirectories with different permissions or parameter settings, you can add a Web. config profile to the corresponding subdirectory and add our new configuration parameters. It is important to note that the subdirectory Web. config inherits all the settings of the parent directory, so if the subdirectory is a different site from the parent directory, the configuration of the parent directory cannot be shared, otherwise it is likely to conflict. For example, many hosts provide a way to bind domain names to subdirectories to be able to build multiple websites, for example, for GoDaddy's Windows host, 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 is analyzed below, and the correct configuration method is given.

View HTTP 500 Error Details

It is easy to see errors on your machine, but when uploaded to a remote host, it is often only a friendly error message that we cannot know the details. To see why the error occurred, you need to display the error details, modify the Web. config file for the root and subdirectory sites, and add the following configuration to the System.webserver node.

Add the following configuration to the system.web node

<customerrors mode= "Off"/>

This will allow you to see a detailed source of the error. The subdirectory site generates a Internal Server error error mostly due to a conflict with the Web. config configuration. For example, in the root directory defines a connectioinstring node named "ConnStr", if you define a connectionstring node in a subdirectory that has the same name as "ConnStr", an error occurs. Because any configuration node does not allow the addition of duplicate keys , you will be prompted for errors such as Dulplicate key. So how do you avoid configuration conflicts between subdirectories and root directories? There are generally two ways to solve One is to modify the Web. config in the root directory so that it eliminates the inheritance of subdirectories and roots, that is, the configuration of the subdirectory Web. config is not affected by the root Web. config, and the second is to modify the Web. config of the subdirectory to create conflicts in all possible and root Web. config Configuration of the add before you clear or remove the configuration information for the root directory. The following is a detailed discussion of the two methods for making subdirectory Web. config independent of the root directory.

Configuration method for subdirectories using standalone web. config

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

<configuration>  <!--... Additional configuration ...-->  <location path= "." Inheritinchildapplications= "false" >    <system.web>    </ system.web>  </location>  <!--... Additional Configuration ...--></configuration>

Here inheritinchildapplications is well understood, that is, whether to allow subdirectory inheritance, the default is true, we modify to false to avoid inheritance. The advantages of this approach are simple, but not flexible enough, and can still be error-faced in the face of a more complex web. config configuration. For example, the root directory and subdirectories have different system.webserver configuration, according to this method in the root directory of the system.webserver add a location limit, unfortunately, there will be a 500 error under IIS7. Of course, there are 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 of the root directory, modify the subdirectory's Web. config. Assuming that the root directory's Web. config has a connection string named BlogEngine, to use another connection string named BlogEngine in the subdirectory, you need to clear the existing connection string (the root directory inherits the ConnectionString settings), clear all the configuration, You can use the clear syntax to clear the configuration of the specified name, using the Remove syntax, as follows

<--root directory web.config--><connectionstrings>  <add name= "BlogEngine" connectionstring= "Data Source= localhost\sqlexpress; Initial catalog=blogengine1; User id=xxx; Password=xxx "Providername=" System.Data.SqlClient "/></connectionstrings>
Web. config for <--subdirectories (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>
<--subdirectory of Web. config (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>

This is just an example of using ConnectionString, which can be applied to all nodes that can be configured, and any configuration node can use the clear and remove nodes to clear the inherited configuration before adding a new configuration. This method is more flexible and preserves some common configurations in the root directory Web. config (without having to reset all). The following is a more complex example of the configuration of the root directory and subdirectories on system.webserver.

<--root directory of Web. config system.webserver configuration node--><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> 
<--subdirectory of the Web. config system.webserver configuration node--><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> 

As you can see, the subdirectory clears all modules (wwwsubdomainmodule,urlrewrite,compressionmodule,referrermodule,scriptmodule) defined by the root directory. Added your own urlrewrite and scriptmodule two module. This configuration eliminates both conflicts and partial inheritance of the configuration (subdirectories are only partially configured and rooted), while the first method does not implement partial inheritance.

Configure a subdirectory of Web. config to eliminate inheritance and create multiple Web sites with virtual directories

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.