不是 Visual Studio 中的 Web 项目的项目的 App.Config 转换? [英] App.Config Transformation for projects which are not Web Projects in Visual Studio?

查看:37
本文介绍了不是 Visual Studio 中的 Web 项目的项目的 App.Config 转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于基于 Visual Studio 2010 Web 的应用程序,我们有配置转换功能,我们可以通过它为不同的环境维护多个配置文件.但同样的功能不适用于 Windows 服务/WinForms 或控制台应用程序的 App.Config 文件.

For Visual Studio 2010 Web based application we have Config Transformation features by which we can maintain multiple configuration files for different environments. But the same feature is not available for App.Config files for Windows Services/WinForms or Console Application.

这里建议了一种解决方法:应用App.Config 的 XDT 魔法.

There is a workaround available as suggested here: Applying XDT magic to App.Config.

然而,这并不简单,需要很多步骤.有没有更简单的方法可以为 app.config 文件实现相同的目标?

However it is not straightforward and requires a number of steps. Is there an easier way to achieve the same for app.config files?

推荐答案

这现在适用于本文中处理的 Visual Studio AddIn:SlowCheetah - Web.config 转换语法现在适用于任何 XML 配置文件.

This works now with the Visual Studio AddIn treated in this article: SlowCheetah - Web.config Transformation Syntax now generalized for any XML configuration file.

您可以右键单击您的 web.config,然后单击添加配置转换."当你这样做时,你会得到一个 web.debug.config 和一个web.release.config.如果你愿意,你可以制作一个 web.whatever.config,作为只要名称与配置文件一致.这些文件只是您想要进行的更改,而不是您的完整副本网络配置.

You can right-click on your web.config and click "Add Config Transforms." When you do this, you'll get a web.debug.config and a web.release.config. You can make a web.whatever.config if you like, as long as the name lines up with a configuration profile. These files are just the changes you want made, not a complete copy of your web.config.

您可能认为您想使用 XSLT 来转换 web.config,但是虽然他们直觉上感觉是对的,但实际上非常冗长.

You might think you'd want to use XSLT to transform a web.config, but while they feels intuitively right it's actually very verbose.

这里有两种转换,一种使用 XSLT,另一种使用 XML文档转换语法/命名空间.就像所有的事情一样XSLT 中有多种方法可以做到这一点,但您已经大致了解了.XSLT是一种广义的树转换语言,而这种部署一个针对常见场景的特定子集进行了优化.但是,该很酷的部分是每个 XDT 转换都是一个 .NET 插件,所以你可以你自己的.

Here's two transforms, one using XSLT and the same one using the XML Document Transform syntax/namespace. As with all things there's multiple ways in XSLT to do this, but you get the general idea. XSLT is a generalized tree transformation language, while this deployment one is optimized for a specific subset of common scenarios. But, the cool part is that each XDT transform is a .NET plugin, so you can make your own.

<?xml version="1.0" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="@*|node()">
  <xsl:copy>           
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>
<xsl:template match="/configuration/appSettings">
  <xsl:copy>
    <xsl:apply-templates select="node()|@*"/>
    <xsl:element name="add">
      <xsl:attribute name="key">NewSetting</xsl:attribute>
      <xsl:attribute name="value">New Setting Value</xsl:attribute>
    </xsl:element>
  </xsl:copy>
</xsl:template>
</xsl:stylesheet>

或者通过部署转换做同样的事情:

Or the same thing via the deployment transform:

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
   <appSettings>
      <add name="NewSetting" value="New Setting Value" xdt:Transform="Insert"/>
   </appSettings>
</configuration>

这篇关于不是 Visual Studio 中的 Web 项目的项目的 App.Config 转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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