开发者

web config <appSettings file="config file in separate projects"></appSettings>

开发者 https://www.devze.com 2023-01-07 10:40 出处:网络
Hi there Im attemting to use the 开发者_如何学Cappsettings in the webconfig to point to a separate config file, the only problem is that the config file is held in a seprate project, is this at all po

Hi there Im attemting to use the 开发者_如何学Cappsettings in the webconfig to point to a separate config file, the only problem is that the config file is held in a seprate project, is this at all possible to reference an app config in a separate project?


It is best not to look outside your project for config. Remember, config files are runtime files, so they often need to be in the same directory or a sub directory of your application root. If you are pointing to a folder outside your root path, you could get into trouble when someone decides to move the physical location of your app and use a virtual directory for it.

If you want to have a config file that is common to multiple projects, I have found that ConfigGen is a good solution. It is a config generator that creates a different (or in your case the same) config file for different machines. It uses the following typical command line, where -s is a settings xml file containing environment-specific differences to be injected into the various output files. -t is a template file, which can contain token placeholders for settings from the settings file. And -o is the output folder:

cfg.exe -s AppSettings.Config.Settings.xls -t AppSettings.Config.Template.xml -o Configs -ves "EmptyString"

In your case, you would have your settings and template files in a shared location and add a pre-build step to import and generate the local AppSettings file. This needs a slightly different command line. I have wrapped it in the BeforeBuild step that you would put into your csproj file:

<Target Name="BeforeBuild">
    <Exec Command="cfg -s X:\sharedConfig\AppSettings.Config.Settings.xls 
     -t X:\sharedConfig\AppSettings.config.template.xml 
     -l -n $(ProjectDir)AppSettings.config" />
</Target>

In this case, X:\sharedconfg\ is the mapped location of your templates and settings, and there are 2 new arguments, -l and -n. -l means, only do the local file, so it looks in your settings file for a row matching the machine that is running the build. If you don't have such a row, it will look for a row named "default", which is what you should have.

The -n argument allows you to specify the exact location that you want to put the output file into.

So if you put this before build step into both your projects, each will generate the same copy of the config.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号