DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • DGS GraphQL and Spring Boot
  • Auto-Instrumentation in Azure Application Insights With AKS
  • Deploying a Scala Play Application to Heroku: A Step-by-Step Guide
  • Why Mocking Sucks

Trending

  • The Modern Data Stack Is Overrated — Here’s What Works
  • Scalable System Design: Core Concepts for Building Reliable Software
  • Unlocking AI Coding Assistants Part 3: Generating Diagrams, Open API Specs, And Test Data
  • Accelerating AI Inference With TensorRT

Working With More Than One web.config Files in an asp.net Application

By 
Jalpesh Vadgama user avatar
Jalpesh Vadgama
·
Mar. 23, 11 · News
Likes (0)
Comment
Save
Tweet
Share
41.3K Views

Join the DZone community and get the full member experience.

Join For Free

Recently one of the readers of my blog asked how we can work with more then one web.config files in an asp.net application. So I decided to blog about that. Here is my response to their inquiry: 

You can work with more than one web.config file in asp.net, but you can not put more than one web.config in each folder. Let’s first understand the hierarchy of web.config and other configuration file settings. On the top of that, every configuration files you will have machine.config file which will have all system wide configuration settings. You can find this file in your OS drive like C: /windows/Microsoft.NET/vFrameworkNumber/Config folder. Here framework number with what ever framework you are using 1.1/2.0 or 4.0. You can override those settings in web.config file at the your application root folder. In the same way, you can add more web.config file in subfolder and can override the setting of parent folder web.config file. So we will create a hierarchy like the one shown below.

Hirerchay

Now let’s Create Project for it. In that I have create two web.config and 2 pages. First I have put the web.config in root folder and then I have put web.config in subfolder. In the same way, I have created a sub folder and then I have put the web.config in sub folder. I have also put one asp.net page in root as well as subfolder to use respective web.config settings. Below is an example of my folder arrangement: 

FolderStructure

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>


After that I have written a code in root asp.net page to print settings from web.config folder like this following.


using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls; namespace MoreWebConfig{  public partial class Root : System.Web.UI.Page  {      protected void Page_Load(object sender, EventArgs e)      {          Response.Write(System.Web.Configuration.WebConfigurationManager.AppSettings.Get("Root"));          Response.Write("");          Response.Write(System.Web.Configuration.WebConfigurationManager.AppSettings.Get("MySetting"));       }  }}


In the same way, I have written code in subfolder like following.

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls; namespace MoreWebConfig.SubFolder{  public partial class SubFolderPage : System.Web.UI.Page  {      protected void Page_Load(object sender, EventArgs e)      {          Response.Write(System.Web.Configuration.WebConfigurationManager.AppSettings.Get("Sub"));          Response.Write("");          Response.Write(System.Web.Configuration.WebConfigurationManager.AppSettings.Get("MySetting"));       }  }}

 

Now, let’s run both pages in browser one by one and you can see root folder application page is fetching settings from root folder web.config while sub folder application page is fetching setting from subfolder web.config even if key ‘mysetting’ is same on both as expected. You can see out put in browser below.

Root.aspx

Root

SubFolderPage.aspx

SubFolder

So it’s very easy to work with multiple web.config. The only limitation of this you can not access sub folder web.config settings from root folder page. Except all you can use anything. Hope you liked it. Stay tuned for more..Happy programming...

application

Published at DZone with permission of Jalpesh Vadgama, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • DGS GraphQL and Spring Boot
  • Auto-Instrumentation in Azure Application Insights With AKS
  • Deploying a Scala Play Application to Heroku: A Step-by-Step Guide
  • Why Mocking Sucks

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: