ASP.NET combine\overlay multiple projects upon build\deploy
I have several ASP.NET MVC web projects that all need access to the same set of configuration files. Since I have multiple configuration files that need to be shared, it 开发者_StackOverflow中文版is not ideal to have multiple copies of them.
With Maven3, I can overlay individual J2EE webapp's on top of others upon build so I could make one project that contains only config files and have it combined into each of the individual web application projects.
Is there any way to achieve this with MSBuild or any other .NET build tool?
My only solution is to use a build event to copy the files from one project into the others, but when creating a deployment package via MSBuild, it only packages files that are a part of the project, and ignores any additional files that are in the folder.
My ultimate goal is to include these config files in the deployment projects that are generated with msbuild /t:Package
(which are eventually deployed to IIS7 with, hopefully, everything they need).
I found a solution to my problem.
- I place my configuration files in a folder in some shared project.
- Right click on the configuration files and select Properties.
- Ensure Build Action is Content and Copy to Output Directory is Copy Always
- Reference "Shared Project" from "Main Project 1" and "Main Project 2"
Now, when "Main Project 1" and "Main Project 2" are built, their reference project ("Shared Project") will also be built and it will have the configuration files published to the /bin directory.
To access these configuration files:
string configPath = HttpRuntime.BinDirectory + string.Format(@"ConfigDirectory\{0}.config", someConfigFileName );
Then you can read this config file (assuming it is in the standard .NET config format) using:
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap
{
ExeConfigFilename = configPath
};
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
var settings = ((AppSettingsSection)config.GetSection("appSettings")).Settings;
精彩评论