使用asp.net根据不同服务器更新web.config
我正在寻找一个简单的&根据不同的环境在 web.config 中更改我的 appsettings
的优雅方法,我有(dev/qa/test/stage/prod),并且我正在手动更改 appsettings
它非常乏味且耗时,我有大约几十种设置......这种情况的最佳方法是什么?我知道一种使用环境变量的解决方案,但想听听最佳实践?
i am looking for a easy & elegant way to change my appsettings
in web.config based on the different environment and i have (dev/qa/test/stage/prod) and i am manually changing the appsettings
and its very tedious and time consuming and i have about dozens of settings.... what is the best approach for this kinda situation? i know one solution using Environment variables
but wants to hear the best practice?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我更喜欢使用 web.config 的外部化部分,如下所示:
然后 appsettings.config 如下所示:
这允许我将应用程序设置保存在不会更改的单独文件中,同时仍然允许我编辑主 web.conifig 进行实际配置部分。
你可以对其他部分做同样的事情 - 连接字符串、system.net(用于电子邮件配置)等。
实际上我不认为 VS2010 的方式很好,因为它需要重新构建才能更改配置。按照我的方式,它根本不需要 VS 就可以使用,所以我可以轻松地手动修改我的所有环境。
I prefer using externalized sections of web.config, like so:
then appsettings.config looks like so:
This allows me to save app settings in separate file that doesn't change, while still allowing me to edit main web.conifig for actual configuration portions.
You can do same thing to other sections - connection strings, system.net (for email config) etc.
I actually don't think VS2010 way is very good, since it requires re-build just to change config. With my way, it's usable without VS at all, so i can easily modify all my environments by hand.
如果可能,请将您的应用程序升级到 asp.net 4。
此版本具有 web.config 转换功能,旨在准确解决此问题:
If possible upgrade your app to asp.net 4.
This version has a web.config transforms feature which is intended to solve exactly this problem:
我通常会执行以下操作:
它需要一些初始设置,但配置完成后,它提供了一种简单且无忧的方法来获取您的配置值。
I will typically do the following:
It takes a bit of initial setup, but once configured, it provides an easy and worry-free method of getting to your config values.
如果您使用 VS2010(NET 工具版本 4),那么您可以进行 Web 配置转换并构建部署包以部署到临时、生产等。您的 ASP.NET 目标版本是什么并不重要。然后部署该包,您将在目标上安装自定义配置文件。不需要运行时来弄清楚。我一直使用 3.5 作为我的 .NET 目标版本,使用 MSBuild 4(VS2010 使用该版本)来执行此操作。我的开发环境具有与生产和暂存环境不同的应用程序设置。
If you use VS2010 (NET tools version 4) then you can do web config transformations and build a deployment package to deploy to staging, production etc. It does not matter what your target version of asp.net is. Then deploy the package and tada you'll have the custom configuration file installed on your target. No need for the run-time to figure it out. I do this all the time using 3.5 as my target version of .NET using MSBuild 4, which VS2010 uses. My development environment has different application settings than my production and staging environments.
最后,如果可能的话,我建议使用 Visual Studio 2010 中的 Web.config 转换功能。
如果 VS2010 不是一个选项或者您不想使用配置转换功能,请查看我不久前提出的这个解决方案。它使用批处理文件和 NANT 脚本的组合,从单个模板和一个或多个替换文件生成配置文件。它不像 Web.config 转换工具那么强大,但它是一个非常有效的替代工具。
http://blog.nathan-taylor.net/2009/10 /webconfig-build-manager.html
Ultimately, I recommend making use of the Web.config Transformation feature in Visual Studio 2010, if possible.
If VS2010 is not an option or you do not wish to use the config transformation feature, take a look at this solution I came up with a while ago. It uses a combination of a batch file and a NANT script to generate config files from a single template and one or more substitution files. It isn't nearly as powerful as the Web.config transformation tool, but it's a pretty effective alternative.
http://blog.nathan-taylor.net/2009/10/webconfig-build-manager.html
在 VS2010 之前,我们实际上将我们的配置命名为 web.prod.config、web.qa.config 等。我想我们有一些远见。
无论如何,在 VS2008 下,我们有一个复制命令,它作为构建步骤的一部分执行,将根据我们正在构建的配置复制适当的配置文件。我们的常规 web.config 指向 dev,我们使用 TFS 部署到其他环境。
Prior to VS2010, we actually named ours web.prod.config, web.qa.config, etc.. I guess we had some great forsight.
In any event, under VS2008 we had a copy command which executed as part of the build step would copy the appropriate config file based on the configuration we were building. Our regular web.config pointed to dev and we used TFS to deploy to the other environments.
我们使用 Nant 来编写构建和部署的脚本。
Nant 会将 web.config 和 app.config 模板复制到项目文件期望的位置。同时,Nant 将令牌替换为适合 QA 环境的值。
We use Nant to script our builds and deploys.
Nant will copy web.config and app.config templates into the locations that the project files expect them. At the same time Nant replaces tokens with appropriate values for the QA environment.
我知道你已经接受了答案,但我采用的是根据不同的环境有不同的配置文件,并这样命名的方法。
例如,我有appsettings.config.debug、appsettings.config.deploy、web.config.debug、web.config.deploy。
然后,我有一个预构建脚本,该脚本运行一个批处理文件,该批处理文件将与当前配置设置相关的配置文件复制到一个不带配置设置后缀的文件中。仅当使用 FC 的文件不同时才会执行此操作。
我的预构建事件命令行代码是:
和
我的 copyifnewer.bat 文件是:
这对我来说非常有用。希望这对某人有帮助。
谢谢,
斯科特
I know you've already accepted an answer, but I use the method of having different config files according to the different environments, and named as such.
For example, I have appsettings.config.debug, appsettings.config.deploy, web.config.debug, web.config.deploy.
Then, I have a pre-build script that runs a batch file that copies the config file related to my current configuration setting into a file without the Configuration setting suffix. It only does this if the files are different using FC.
My pre-build event command-line code is:
and
And my copyifnewer.bat file is:
This works great for me. Hope this helps someone.
Thanks,
Scott