Sharing app setting between two web projects - c#

I have two separate solutions with web projects. I need to use one app setting in both. I don't want to duplicate this value in two web.config files - want to store it in one place (and not in machine.config!). These two web projects have other different appSettings and their web.config files are completely different.
Is it some way to share somehow just one appSetting between two web projects?

This link
explains how you can refer to another config file for some of the settings. The config file doesn't have to be stored in the same folder as your web.config file so you can share it with other projects.

Related

Brand specific web.config sections automation

I am doing some research on how to make web.config dynamic per environment and brand. We have web.config different for different environments and brands.
Right now we make copy of it store a separate files and finally pick it manually and deploy.
I am finding various arcticles to do this and one the below has one solution.
http://www.hanselman.com/blog/ManagingMultipleConfigurationFileEnvironmentsWithPreBuildEvents.aspx
I don't want any code or anything like but need some references if there are any other best industry practices
See http://msdn.microsoft.com/en-us/library/dd465326(v=vs.110).aspx for web.config transformations. Same concept. You have a base config file and then have specific nested config files per environment, brand or both. Depending how you deploy your application can affect how many web.config you have. In newer visual studio you cannpreview the changes as well by clicking on the nested web.config in solution explorer
App.config transformations aren't supported out of the box but with some msbuild events that's how wendo these ones

Copy settings from one Web.config to another except AppSettings and ConnectionStrings

Is it possible, through code, to copy settings from one Web.config file to another (except AppSettings and ConnectionStrings)?
I have a situation where a single MVC3 project has been deployed to multiple servers in different locations. There is an auto-updater on all of these that will pull in the latest version. Typically when this would run, it would only overwrite the application folders and NOT the Web.config file.
I just upgraded the project to MVC4. This changes basically everything in the Web.config file except the AppSettings and ConnectionStrings. All of the installations of this project would have slightly different values here.
How would I go about writing some code that will update the Web.config file, but preserve all of the AppSettings and ConnectionStrings?
.NET provides ways to get configuration from other config file. Refer this article:http://blog.andreloker.de/post/2008/06/Keep-your-config-clean-with-external-config-files.aspx
Basically, you can use "configSource" attribute to define which config file to refer. Note that the configuration file should be in same directory. if not, Refer here to solve the problem. .NET Config Files configSource outside the application directory folder

Common app.config for multiple applications

I have several C# console applications, which need to have the same set of settings. I want to avoid duplicity and avoid separate app.config for each application.
Is there any way to read a common app.config file (say common.config) for applications (app1.exe, app2.exe).
Create one file called app.config. Put it in some place outside of your projects' directories, like up in the solution directory. Add it to your projects as a linked item with a relative path to the file. Set the right build action for this item (application configuration) in each project.
Now when each project builds, the file will be copied to the project's output dir with the right name.
You can load an external app.config using the code below:
config = ConfigurationManager.OpenExeConfiguration(Path.Combine("C:\test\root", "Master.exe"));
string logpath = config.AppSettings.Settings["Log.Path"].Value;
And save settings as so:
config = ConfigurationManager.OpenExeConfiguration(Path.Combine("C:\test\root", "Master.exe"));
config.AppSettings.Settings["Log.Path"].Value = "C:\newpath";
config.Save();
You might have to have a master config within one of the applications and point the rest to this. Typically this method is considered bad practice though. There might be issues with different applications locking the file.
#Ran's answer is an option, but each application will still have its own config file after you build. At compile time they will be the same, but at deploy time they are copies.
You can also open one application's config file from another application using:
ConfigurationManager.OpenExeConfiguration(string)
You can have an external config file that all applications reference using:
ConfigurationManager.OpenMappedExeConfiguration
And there's the option to using the Machine config file using:
ConfigurationManager.OpenMachineConfiguration()

How to handle two or more app configs?

Lets imagine I have three projects in my solution. The first one is executeble and the other two are class libraries.
Every project has it's own app.config file which contain some httpBinding data, some user settings and connection strings.
Then I build the solution and all I get (in the EXE's bin directory) is the only app.config file with the XML elements wich are related to the executable project.
So, the question. How do I suppose to use that another two configs (which are successefully built to their corresponding project bin folders?
You need to put the settings in the dll libraries in the setting file of executable. Or you can make a separate libraries for handling settings of all the projects in the solution. The default setting files look in to app.config.

c# multiple web.config files in a single project under different folders?

Can we have multiple web.config files in a single project in c#?
if yes then what is its benefit?
Say I have a project ABC that has three folders under it.
Now ABC has its web.config file, so does each folder under ABC.
so there are total 4 web.config files.
So what will be the output when I run the project?
Yes you can have multiple web.config files in the separate folders. They can be used to enable/disable modules on a per folder basis. You can even set up each of those folders as separate web sites, even sub-sites, that use some modules from the parent web.config and some from the sub-site web.config.
I wrote up a blog post in the past about using BlogEngine.NET as the main site and configuring the web.config file in sub-sites. It might be of some help to you: http://markschlegel.com/post/2011/11/26/BlogEngineNET-a-sub-site!.aspx
Sure you can. An example of what this is used for is the default MVC 3 project template. There is one web.config for the site, and there is one inside the Views folder to deal with rendering the views.
Yes you can. The ASP.NET MVC project templates do this to restrict browsing the Views folder.

Categories