Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Web.config location element demystified
Thomas Ardal
Thomas Ardal

Posted on • Originally published atblog.elmah.io

     

Web.config location element demystified

I haven't actually met a lot of people who knew the details about the hierarchy ofWeb.config files and how to utilize thelocation element. In fact, I've heard multiple people ask "why is there aWeb.config file in theViews folder of my MVC application?". Even ASP.NET Core projects require aWeb.config file (generated on publish) and you can still use some of the features in there to control the execution of your ASP.NET Core website. When you have finished this post you should be an expert.

If we forget aboutWeb.config being an XML file with the disadvantages this causes, it is a pretty powerful feature when hosting a website on IIS.

Web.config hierarchy

Before we start discussing thelocation element, I want to introduce you to the concept of having multipleWeb.config files. You already know the one generated in the root of a new project:

This file controls a lot of features in this application, like application settings and HTTP modules and handlers. In a real application you typically have subdirectories, which will allow you to handle requests to an URL like/sub/. The settings inside theWeb.config file are available for both the root as well as any subdirectories. Let's say you want some settings specific and visible for/sub/ only, you can place an overwritingWeb.config file inside thesub folder. To illustrate, let's create a simple example whereappSettings are overwritten.

For the example, I've created a folder namedsub and a newDefault.aspx file inside that folder. This example is using WebForms but it could be anything on top of IIS. Inside thesub folder, I'm also placing a newWeb.config file:

In theappSettings element of theWeb.config file located in the root, I'll create a new setting namedMessage:

<configuration><appSettings><addkey="Message"value="Frontpage"/></appSettings>  ...</configuration>
Enter fullscreen modeExit fullscreen mode

In theappSettings element of theWeb.config file located in theSub folder, I'll create a similar setting also namedMessage:

<configuration><appSettings><addkey="Message"value="Subpage"/></appSettings></configuration>
Enter fullscreen modeExit fullscreen mode

Notice how the first file contains more configuration (represented by the three dots) and the configuration in theWeb.config file in theSub folder only contains the overwriting config.

To test that everything is working, replace the content of theDefault.aspx file in theSub folder with the following markup:

<%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="Default.aspx.cs"Inherits="WebApplication9.sub.Default"%><!DOCTYPE html><htmlxmlns="http://www.w3.org/1999/xhtml"><body><h1>Hello from<%=ConfigurationManager.AppSettings["Message"]%></h1></body></html>
Enter fullscreen modeExit fullscreen mode

You already guessed it. When navigating to/sub/, the message isHello from Subpage:

Location element

Finally! This is why you came here in the first place, right? While havingWeb.config files spread across folders, sometimes it is just easier to configure everything in the same place. Thelocation element lets us do exactly that. By specifying alocation start and end tag inside the web application, we have a "mini"Web.config file included in the normalWeb.config. Confused? Let's re-create the previous scenario, but with everything included in the sameWeb.config file:

<configuration><appSettings><addkey="Message"value="Frontpage"/></appSettings><locationpath="Sub"><appSettings><addkey="Message"value="Subpage"/></appSettings></location>  ...</configuration>
Enter fullscreen modeExit fullscreen mode

If you are creating your own example, make sure to delete theWeb.config file in theSub folder.

Inside thelocation element, I've added a newappSettings element. In fact, the possible nested elements oflocation are the ones from theconfiguration element. This means you can add asystem.webServer element or any other element allowed for aWeb.config file insidelocation.

allowOverride

Besides thepath attribute on thelocation element, there's another interesting attribute namedallowOverride. With this attribute, you can lock the content of thelocation element. Let's create an example illustrating how this works.

You can combine the use of both thelocation element and aWeb.config file in a subdirectory. ASP.NET will always use the configuration closest to the page you are requesting. In the scenario where we haveappSettings specified in three different locations, when requesting/Sub/ IIS will look for configuration in the following order:

  1. In aWeb.config file in theSub folder.
  2. In alocation element withpath set toSub in theWeb.config file in the root folder.
  3. In theWeb.config file in the root folder.

To test this, add theWeb.config file from the first example to theSub folder and replace the content with this:

<?xml version="1.0" encoding="utf-8" ?><configuration><appSettings><addkey="Message"value="Web.config in Sub folder"/></appSettings></configuration>
Enter fullscreen modeExit fullscreen mode

When running the application, you will see the new message:

To lock the configuration in theWeb.config file in the root directory, addallowOverride="false":

<configuration><appSettings><addkey="Message"value="Frontpage"/></appSettings><locationpath="Sub"allowOverride="false"><appSettings><addkey="Message"value="Subpage"/></appSettings></location>  ...</configuration>
Enter fullscreen modeExit fullscreen mode

Launching the project will now cause an exception, since theWeb.config file in theSub folder is trying to override settings that it isn't allowed to:

Wildcard and regular expression

I've seen question after question, requesting the use of wildcards, regular expressions and similar in thepath attribute of thelocation element. Like being able to target something like all sub-directories with a specific naming pattern:

<locationpath="languages/*">
Enter fullscreen modeExit fullscreen mode

I'm sorry to be the one to break it to you. It's not possible. Thepath attribute requires an absolute path to an existing file or directory.

Would your users appreciate fewer errors?

elmah.io is the easy error logging and uptime monitoring service for .NET. Take back control of your errors with support for all .NET web and logging frameworks.

➡️Error Monitoring for .NET Web Applications ⬅️

This article first appeared on the elmah.io blog athttps://blog.elmah.io/web-config-location-element-demystified/

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Entrepreneur and builder of online things. Bootstrapping elmah.io. Dad to 👦👧 Powered by ☕&🍜 Likes Star Wars, stonk trading, 3D printing, and retro games.
  • Location
    Aarhus, Denmark
  • Work
    Founder at elmah.io
  • Joined

More fromThomas Ardal

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp