简体   繁体   中英

Solution wide app.config / web.config?

I have a solution with one asp.net-mvc-2 project, one web service, one class library and three normal desktop apps. Currently I need to copy all app.config settings from one project to the other. That is a nightmare if I want to change something.

I know there is a solution to store all these information in the global .config files, but I am looking for a solution to store the values dedicated to my solution not to my machine. Is that possible?

You can add a single .config file to one of your projects, and then choose Add -> Existing Item... -> Add As Link to add a reference to that file to your other projects. They will all build with the file as if it was their own copy, but there will really only be one copy, and you will only have to make changes in a single place.

Assuming you could have a path where both your website and webservice could access it, then you could potentially use the appsettings " file " attribute to have the settings stored in one common place. Refer http://www.codeproject.com/KB/dotnet/appsettings_fileattribute.aspx Hope this helps!

I would comment on the answer @Sahuagin gave, but I don't have enough reputation. This does work with App.config files, but you have to add a text file, then call it App.config. After that just declare the XML (as seen below)

<configuration>
   <!-- add you values or whatever here-->
</configuration>

Then do as @Sahuagin said and add it as a link to your solution. It works a treat.

I have found a really simple solution here :

1. Create a file CommonSettings.config In your Common, Class library project.

2. Put your common setting in this file:

<appSettings>
    <add key="someCommonSetting" value="some value"></add>
    <!-- more setting here -->
</appSettings>

Note: <appSetting> has to be the root element in your CommonSettings.config (don't put it under <configuration> )

3. Make sure CommonSettings.config file is copied to output directory:

在此处输入图片说明

4. In all other project's App.Config/Web.config files, add the above common settings

That's it... You common settings will be included in every other config file.

<appSettings file="CommonSettings.config">
    <add key="Value1" value="123" />
</appSettings>  

Note:

For this approach to work, the shared config file should be copied to the project's output directory so it is adjacent to the regular App/Web.config file. Add the existing shared .config file to the project as a Linked file and set it to 'Copy if newer'. You should see something similar to this in your .csproj file:

 <None Include="..\\CommonConnectionStrings.config"> <Link>CommonConnectionStrings.config</Link> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </None>

You can do the same for ConnectionString , copy all your connection strings in CommonConnectionStrings.config , and in other App.Config/Web.Config, reference it like this:

<connectionStrings configSource="CommonConnectionStrings.config" />

Note: If you are using this solution for connectionStrings , you cannot have a project specific connection string, all of the connection strings will be copied from the common 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