在开发和生产环境中使用不同的 Web.config [英] Using different Web.config in development and production environment

查看:22
本文介绍了在开发和生产环境中使用不同的 Web.config的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的 ASP.NET 应用程序中使用不同的数据库连接字符串和 SMTP 服务器地址,具体取决于它是在开发环境还是生产环境中运行.

I need to use different database connection string and SMTP server address in my ASP.NET application depending on it is run in development or production environment.

应用程序通过 WebConfigurationManager.AppSettings 属性.

The application reads settings from Web.config file via WebConfigurationManager.AppSettings property.

我使用 Build/Publish 命令通过 FTP 将应用程序部署到生产服务器,然后用正确的手动替换远程 Web.config.

I use Build/Publish command to deploy the application to production server via FTP and then manually replace remote Web.config with correct one.

是否有可能以某种方式简化部署过程?谢谢!

Is it possible somehow simplify the process of deployment? Thanks!

推荐答案

在 Visual Studio 2010 及更高版本中,您现在可以根据构建配置对 web.config 应用转换.

In Visual Studio 2010 and above, you now have the ability to apply a transformation to your web.config depending on the build configuration.

创建web.config时,可以在解决方案资源管理器中展开该文件,会看到两个文件:

When creating a web.config, you can expand the file in the solution explorer, and you will see two files:

  • Web.Debug.Config
  • Web.Release.Config

它们包含可用于的转换代码

They contain transformation code that can be used to

  • 更改连接字符串
  • 删除调试跟踪和设置
  • 注册错误页面

请参阅 Web 应用程序项目部署的 Web.config 转换语法 在 MSDN 上了解更多信息.

See Web.config Transformation Syntax for Web Application Project Deployment on MSDN for more information.

尽管官方不支持,但也可以对非 Web 应用程序 app.config 文件应用相同类型的转换.请参阅 Phil Bolduc 博客,了解如何修改您的项目文件以向 msbuild 添加新任务.

It is also possible, albeit officially unsupported, to apply the same kind of transformation to an non web application app.config file. See Phil Bolduc blog concerning how to modify your project file to add a new task to msbuild.

这是一个长期存在的对 Visual Studio 用户语音的请求.

一个 Visual Studio 2010 及更高版本的扩展,"SlowCheetah," 可用于为任何人创建变换配置文件.从 Visual Studio 2017.3 开始,SlowCheetah 已集成到 IDE 中,代码库由 Microsoft 管理.这个新版本还支持JSON转换.

An extension for Visual Studio 2010 and above, "SlowCheetah," is available to take care of creating transform for any config file. Starting with Visual Studio 2017.3, SlowCheetah has been integrated into the IDE and the code base is being managed by Microsoft. This new version also support JSON transformation.

这篇关于在开发和生产环境中使用不同的 Web.config的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆