多个 WebConfig 合二为一

发布于 2024-10-06 10:47:08 字数 146 浏览 1 评论 0原文

我们的网站有一个生产服务器和一个开发服务器。我每个都有不同的设置和许可证信息,并且保存在 2 个不同的 web.config 文件中。

有没有一种方法可以将它们合并到一个 web.config 文件中?这样我在发布网站时就不会用开发版本覆盖生产版本(反之亦然)。

We have a production server and a development server for our site. I have different settings and license information for each and that is held in 2 different web.config files.

Is there a way to consolidate those to one web.config file? So that I don't overwrite production's with development's (or vice versa) when I publish the site.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

聽兲甴掵 2024-10-13 10:47:08

使用 VS 2010,您可以通过 web.config 转换和构建配置来完成此操作。 Scott Guthrie 对此写道:

http:// /weblogs.asp.net/scottgu/archive/2010/07/29/vs-2010-web-deployment.aspx

我认为这基本上只需创建额外的配置文件(例如 Web.Debug. config),VS 会自动将它们“链接”到 IDE 中的主 web.config,但我无法在我现在使用的计算机上验证它,因为它没有 VS 2010。

您也可以相当轻松地做到这一点在旧版本的 VS 中可以使用构建事件,但它并不那么好。斯科特实际上也写过这一点:

http://weblogs.asp.net/scottgu/archive/2007/09/21/tip-trick-automating-dev-qa-staging- and-product-web-config-settings-with-vs-2005.aspx

(摘自 Scott 的博客文章)这里是您执行此操作所采取的高级步骤。它们可以与 VS 2005 和 VS 2008 一起使用。

  1. 使用 ASP.NET Web 应用程序项目(具有基于 MSBuild 的项目文件)
  2. 打开 VS 配置管理器并为您的项目创建新的“Dev”、“QA”、“Staging”构建配置解决方案
  3. 在项目中添加新的“web.config.dev”、“web.config.qa”和“web.config.staging”文件,并自定义它们以包含应用程序的模式特定配置设置
  4. 添加新的“预构建事件” " 命令到您的项目文件,该文件可以在每次构建项目时使用适当的模式特定版本自动复制项目中的 web.config 文件(例如:如果您的解决方案处于“Dev”配置中,它将复制web.config.dev 设置到主 web.config 文件)。

With VS 2010, you can do this with web.config transformations and build configurations. Scott Guthrie wrote about this:

http://weblogs.asp.net/scottgu/archive/2010/07/29/vs-2010-web-deployment.aspx

I think this basically works by you just creating your additional config files (such as Web.Debug.config) and VS automatically "links" them to your main web.config in the IDE, but I can't verify it at the computer I am using right now since it doesn't have VS 2010.

You can also do it fairly easily with build events in older versions of VS, but it isn't as nice. Scott actually wrote about this too:

http://weblogs.asp.net/scottgu/archive/2007/09/21/tip-trick-automating-dev-qa-staging-and-production-web-config-settings-with-vs-2005.aspx

(Taken from Scott's blog post) Here the high-level steps you take to do this. They work with both VS 2005 and VS 2008.

  1. Use ASP.NET Web Application Projects (which have MSBuild based project files)
  2. Open the VS Configuration Manager and create new "Dev", "QA", "Staging" build configurations for your project and solution
  3. Add new "web.config.dev", "web.config.qa", and "web.config.staging" files in your project and customize them to contain the app's mode specific configuration settings
  4. Add a new "pre-build event" command to your project file that can automatically copy over the web.config file in your project with the appropriate mode specific version each time you build the project (for example: if your solution was in the "Dev" configuration, it would copy the web.config.dev settings to the main web.config file).
孤凫 2024-10-13 10:47:08

您可以创建 Web 部署项目 (VS2008VS2010) 这是一个 XML MSBuild 文件,允许您在构建特定配置文件时将 XML 节点替换/插入/替换到配置文件中。您可能会花费半天到一天的时间来正确配置它,但这些时间是非常值得的,因为它可以为您省去很多部署麻烦(也让您更接近一键部署)。

注意:对于 VS2008 Web 部署项目,您可能需要使用 MSBuild 社区任务。我不知道 VS2010 的这些任务的状态如何 - 是否仍然需要它们。

对于使用 MSBuild 社区任务(我们在 VS2010 下使用)的 VS2008 Web 部署任务,它看起来有点像......

<!-- ... -->
<Target Name="AfterBuild">
    <XmlMassUpdate Condition="'$(Configuration)|$(Platform)' == 'Release - Dev|AnyCPU'" ContentFile="$(OutputPath)\web.config" SubstitutionsFile="$(OutputPath)\web.Substitutions.config" ContentRoot="/" SubstitutionsRoot="/configuration/substitutions/dev" />
</Target>
<!-- ... -->

在我们的 Web 应用程序目录中,我们有一个文件 web.Substitutions.config ,它看起来是有点像...

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:xmu="urn:msbuildcommunitytasks-xmlmassupdate">
  <substitutions>

    <!-- dev settings-->
    <dev>
      <configuration>
        <appSettings file="">
          <add xmu:key="key" key="some_setting" value="a_value_is_here"/>
                </appSettings>
      </configuration>
    </dev>
    <prod> <!-- you get the idea --></prod>
   </substitutions>
</configuration>

值得一提的是,我们使用 Team City 来运行我们的 Web 部署项目,无需额外配置必要的并且它可以顺利工作。 Hudson 也是同样的方式(并且设置起来更容易一些)。

You can create a web deployment project (VS2008, VS2010) which is an XML MSBuild file that lets you replace/insert/substitute XML nodes into the configuration file when you build for a particular profile. You'll probably spend a half a day to a day getting it configured correctly but the time is well worth it as it can save you a lot of deployment headaches (also puts you one step closer to one-click deployments).

Note: for the VS2008 web deployment projects you will probably want to use the MSBuild Community Tasks. I don't know what the status is for VS2010 with these tasks - whether or not they are still required.

For our VS2008 web deployment tasks using the MSBuild Community Tasks (which we use under VS2010) it looks a little like...

<!-- ... -->
<Target Name="AfterBuild">
    <XmlMassUpdate Condition="'$(Configuration)|$(Platform)' == 'Release - Dev|AnyCPU'" ContentFile="$(OutputPath)\web.config" SubstitutionsFile="$(OutputPath)\web.Substitutions.config" ContentRoot="/" SubstitutionsRoot="/configuration/substitutions/dev" />
</Target>
<!-- ... -->

In our web application's directory we have a file web.Substitutions.config which looks a bit like...

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:xmu="urn:msbuildcommunitytasks-xmlmassupdate">
  <substitutions>

    <!-- dev settings-->
    <dev>
      <configuration>
        <appSettings file="">
          <add xmu:key="key" key="some_setting" value="a_value_is_here"/>
                </appSettings>
      </configuration>
    </dev>
    <prod> <!-- you get the idea --></prod>
   </substitutions>
</configuration>

Worth mentioning that we use Team City to run our web deployment projects with no additional configuration necessary and it works without a hitch. Hudson is the same way (and a bit easier to setup).

风轻花落早 2024-10-13 10:47:08

这只是一种可能性,但您可以合并它们,根据上下文为值提供适当的名称,并使用代码中的编译常量有条件地访问每个元素。例如,在您的 web.config 中,您可能有...

<connectionStrings>
    <add name="DevelopmentConnection" connectionString="DevelopmentConnectionString" />
    <add name="DeploymentConnection" connectionString="DeploymentConnectionString" />
</connectionStrings>

然后,在您可能有的 Configuration 类中...

public static class Configuration
{
    public string ConnectionString
    {
        get
        {
            #if DEBUG
                return ConfigurationManager.ConnectionStrings["DevelopmentConnection"].ConnectionString;
            #else
                return ConfigurationManager.ConnectionStrings["DeploymentConnection"].ConnectionString;
            #endif
        }
    }
}

现在,考虑到定义了 DEBUG 常量,那么,在每个环境构建时,使用此属性将通过适当的设置。

Just one possibility, but you could merge them giving appropriate names to values based on the context and access each element conditionally using compilation constants in code. For instance, in your web.config you may have...

<connectionStrings>
    <add name="DevelopmentConnection" connectionString="DevelopmentConnectionString" />
    <add name="DeploymentConnection" connectionString="DeploymentConnectionString" />
</connectionStrings>

Then, within, let's say, a Configuration class you may have...

public static class Configuration
{
    public string ConnectionString
    {
        get
        {
            #if DEBUG
                return ConfigurationManager.ConnectionStrings["DevelopmentConnection"].ConnectionString;
            #else
                return ConfigurationManager.ConnectionStrings["DeploymentConnection"].ConnectionString;
            #endif
        }
    }
}

Now, considering the DEBUG constant is defined, then, when building per environment, using this property would pull through the appropriate setting.

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