在开发和生产环境中使用不同的 Web.config

编程入门 行业动态 更新时间:2024-10-24 16:22:31
本文介绍了在开发和生产环境中使用不同的 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

本文发布于:2023-11-01 01:17:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1547826.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:环境   Web   config

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!