简体   繁体   中英

Multiple Web.Config files - from a code point-of-view

ASP.NET allows for Web.Config files at sub-levels within a site structure. However, I can't find any articles discussing how this looks from code.

In the Orchard CMS there are config files all over the shop. There's even a config file in a folder containing only .CSS files! I'm new to larger-scale ASP.NET apps so...

Can someone just tell me if I'm right in my assumptions.

  • The config file can affect server settings, and hence how a server processes a request to a resource further down a site structure.

  • From a code point of view, if the same line of code in the same class queries the config file during a request for http://level1.resource then it could read a different value to when the same code executes during a request for http://level1/level2.resource (if there is a web.config at level2)

    • Overall, the way it works is based on the current request path.

Right?

you can use the multiple web.config files in the sub-folders level. Each folder will contains its own web.config. Multiple Web.config files can't be used at same level.

Below is code for root folder web.config

<?xml version="1.0"?>
<configuration>
<system.web>
    <compilation debug="true" targetFramework="4.0" />
</system.web>
 <appSettings>
 <add key="root" value="This is from root web.config"></add>
 <add key="MySetting" value="This my settings is from root web.config"></add>
 </appSettings>
</configuration>

and following is code for sub folder web.config.

<?xml version="1.0"?>
<configuration>
<system.web>
</system.web>
<appSettings>
 <add key="sub" value="This is from sub web.config settings"></add>
 <add key="MySetting" value="This my settings is from sub folder web.config"></add>
</appSettings>
</configuration>

In C# , you can access the settigns of different configuration files as below. System.Web.Configuration.WebConfigurationManager.AppSettings.Get("Root"); System.Web.Configuration.WebConfigurationManager.AppSettings.Get("MySetting")

This is an old question, but I just asked myself the same one specifically in the Orchard CMS context as per the original post, and I've got an Orchard specific answer.

In the root of Orchard CMS, there's a web.config that want to prevent all users from requesting individual static files. For example, you don't want people to download placement.info or theme.txt from the Theme folders. This is a good "block everything, allow what you need" approach.

<handlers accessPolicy="Script">
    <!-- Clear all handlers, prevents executing code file extensions or returning any file contents. -->
    <clear />
    <!-- Return 404 for all requests via a managed handler. The URL routing handler will substitute the MVC request handler when routes match. -->
    <add name="NotFound" path="*" verb="*" type="System.Web.HttpNotFoundHandler" preCondition="integratedMode" requireAccess="Script" />
    [...]
</handlers>

The thing is, in subfolders such as Scripts (containing static js files), Styles (containing only lots of static css files), or Content (contains static images for example), you of course want to allow the web browser to request an individual file. So in these subfolders, you have an extra web.config that looks like this:

<handlers accessPolicy="Script,Read">
    <!-- For any request to a file exists on disk, return it via native http module. AccessPolicy="Script" above is to allow for a managed 404 page. -->
    <add name="StaticFile" path="*" verb="*" modules="StaticFileModule" preCondition="integratedMode" resourceType="File" requireAccess="Read" />
</handlers>

PS: I'm currently playing with Themes, and for some reason I've had to add <remove name="StaticFile"/> before each <add name="StaticFile"...> in all these child web.config.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM