使用asp.net根据不同服务器更新web.config

发布于 2024-10-06 03:25:14 字数 203 浏览 0 评论 0原文

我正在寻找一个简单的&根据不同的环境在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

苄①跕圉湢 2024-10-13 03:25:15

我更喜欢使用 web.config 的外部化部分,如下所示:

<appSettings configSource="appsettings.config" />

然后 appsettings.config 如下所示:

<appSettings>
   <add key ...>
</appSettings>

这允许我将应用程序设置保存在不会更改的单独文件中,同时仍然允许我编辑主 web.conifig 进行实际配置部分。
你可以对其他部分做同样的事情 - 连接字符串、system.net(用于电子邮件配置)等。

实际上我不认为 VS2010 的方式很好,因为它需要重新构建才能更改配置。按照我的方式,它根本不需要 VS 就可以使用,所以我可以轻松地手动修改我的所有环境。

I prefer using externalized sections of web.config, like so:

<appSettings configSource="appsettings.config" />

then appsettings.config looks like so:

<appSettings>
   <add key ...>
</appSettings>

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.

岁月静好 2024-10-13 03:25:15

如果可能,请将您的应用程序升级到 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:

北笙凉宸 2024-10-13 03:25:15

我通常会执行以下操作:

  1. 包含每个配置值的多个版本,每个环境一个版本
  2. 创建一个小类来读取我的应用程序设置。在此类中,创建一个检测当前环境的函数。
  3. 对于每个设置,创建一个具有开关的访问器属性,该开关使用环境检测功能来确定要返回哪个值。

它需要一些初始设置,但配置完成后,它提供了一种简单且无忧的方法来获取您的配置值。

I will typically do the following:

  1. Include multiple versions of each config value, one for each environment
  2. Make a small class for reading my app settings. In this class, create a function that detects the current environment.
  3. For each setting, create an accessor property that has a switch which uses the environment detection function to determine which value to return.

It takes a bit of initial setup, but once configured, it provides an easy and worry-free method of getting to your config values.

假面具 2024-10-13 03:25:15

如果您使用 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.

攒眉千度 2024-10-13 03:25:15

最后,如果可能的话,我建议使用 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

地狱即天堂 2024-10-13 03:25:15

在 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.

风向决定发型 2024-10-13 03:25:15

我们使用 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.

橘虞初梦 2024-10-13 03:25:15

我知道你已经接受了答案,但我采用的是根据不同的环境有不同的配置文件,并这样命名的方法。

例如,我有appsettings.config.debug、appsettings.config.deploy、web.config.debug、web.config.deploy。

然后,我有一个预构建脚本,该脚本运行一个批处理文件,该批处理文件将与当前配置设置相关的配置文件复制到一个不带配置设置后缀的文件中。仅当使用 FC 的文件不同时才会执行此操作。

我的预构建事件命令行代码是:

"$(ProjectDir)copyifnewer.bat" "$(ProjectDir)web.config.$(ConfigurationName)" "$(ProjectDir)web.config"

"$(ProjectDir)copyifnewer.bat" "$(ProjectDir)appsettings.config.$(ConfigurationName)" "$(ProjectDir)appsettings.config"

我的 copyifnewer.bat 文件是:

echo Config Maintenance
echo Today's date of %date:~-4,4%\%date:~-10,2%\%date:~-7,2%
rem echo Comparing two files: %1 with %2
echo .
if not exist %1 goto File1NotFound
if not exist %2 goto File2NotFound

fc %1 %2 
if %ERRORLEVEL%==0 GOTO NoCopy

echo These files are NOT the same.  Copying %1 over %2
copy %1 %2 /y & goto END

:NoCopy
echo These files are the same.  Nothing copied.
goto END

:File1NotFound
echo %1 not found.
goto END

:File2NotFound
copy %1 %2 /y
goto END

:END
echo Done.
echo .

这对我来说非常有用。希望这对某人有帮助。

谢谢,
斯科特

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:

"$(ProjectDir)copyifnewer.bat" "$(ProjectDir)web.config.$(ConfigurationName)" "$(ProjectDir)web.config"

and

"$(ProjectDir)copyifnewer.bat" "$(ProjectDir)appsettings.config.$(ConfigurationName)" "$(ProjectDir)appsettings.config"

And my copyifnewer.bat file is:

echo Config Maintenance
echo Today's date of %date:~-4,4%\%date:~-10,2%\%date:~-7,2%
rem echo Comparing two files: %1 with %2
echo .
if not exist %1 goto File1NotFound
if not exist %2 goto File2NotFound

fc %1 %2 
if %ERRORLEVEL%==0 GOTO NoCopy

echo These files are NOT the same.  Copying %1 over %2
copy %1 %2 /y & goto END

:NoCopy
echo These files are the same.  Nothing copied.
goto END

:File1NotFound
echo %1 not found.
goto END

:File2NotFound
copy %1 %2 /y
goto END

:END
echo Done.
echo .

This works great for me. Hope this helps someone.

Thanks,
Scott

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文