Using different Web.config in development and production environment

前端 未结 10 2094
梦如初夏
梦如初夏 2020-11-22 12:50

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.

<
10条回答
  •  清酒与你
    2020-11-22 13:33

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

    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

    • Change the connection string
    • Remove debugging trace and settings
    • Register error pages

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

    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.

    This is a long withstanding request on the Visual Studio Uservoice.

    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.

提交回复
热议问题