本文介绍了NuGet 可以编辑配置文件或仅添加到其中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在为我的公司开发 NuGet 包,其中一项要求是能够更新我们的一些配置文件.

I've been working on a NuGet package for my company and one of the requirements is being able to update some of our config files.

我知道可以添加到配置文件中,但是可以编辑吗?

I know it's possible to add to a config file, but is it possible to edit one?

示例:

<add name="conn" connectionString="Data Source=.\;Initial Catalog=DB;Integrated Security=True" />

更改为以下

<add name="conn" connectionString="Data Source=.\;Initial Catalog=DB;User ID=ex;Password=example" />

推荐答案

从 NuGet 2.6 及更高版本开始,您实际上可以使用 Visual Studio 中用于 Web.config 转换的 XDT 语法来转换 Web.config 文件.

As of NuGet 2.6 and above, you can actually transform Web.config files using the XDT syntax that is used for Web.config transforms in Visual studio.

参见 http://docs.nuget.org/docs/创建包/配置文件和源代码转换:

支持 XML 文档转换 (XDT)

Support for XML-Document-Transform (XDT)

从 NuGet 2.6 开始,支持 XDT 转换项目内的 XML 文件.XDT 语法可以在包的 Content 文件夹下的 .install.xdt 和 .uninstall.xdt 文件中使用,它们将分别在包安装和卸载时应用.

Starting with NuGet 2.6, XDT is supported to transform XML files inside a project. The XDT syntax can be utilized in the .install.xdt and .uninstall.xdt file(s) under the package's Content folder, which will be applied during package installation and uninstallation time, respectively.

例如,要将 MyNuModule 添加到 web.config 文件(如上图所示),可以在 web.config.install.xdt 文件中使用以下部分:

For example, to add MyNuModule to web.config file like what's illustrated above, the following section can be used in the web.config.install.xdt file:

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <system.webServer>
        <modules>
            <add name="MyNuModule" type="Sample.MyNuModule" xdt:Transform="Insert" />
        </modules>
    </system.webServer>
</configuration>

另一方面,要在卸载包期间仅删除 MyNuModule 元素,可以在 web.config.uninstall.xdt 文件中使用以下部分:

On the other hand, to remove only the MyNuModule element during package uninstall, the following section can be used in the web.config.uninstall.xdt file:

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <system.webServer>
        <modules>
            <add name="MyNuModule" xdt:Transform="Remove" xdt:Locator="Match(name)" />
        </modules>
    </system.webServer>
</configuration>

这篇关于NuGet 可以编辑配置文件或仅添加到其中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 04:30