【问题标题】:Nuget Package - Web.config.transform AddNuget 包 - Web.config.transform 添加
【发布时间】:2013-06-13 16:33:42
【问题描述】:

我正在创建一个 NuGet 包,并希望它使用某些设置更新 Web 项目 Web.Config 文件。我正在使用 web.config.transform 来编辑应用程序的 web.config 文件。当我简单地添加 appSettings 时它运行良好 - 像这样:

<configuration>
  <appSettings>
    <add key="WebPToolFolder" value ="~/Tools"/>
    <add key="ImagesFolder" value ="~/Content/themes/base/images"/>
  </appSettings>
</configuration>

但是,如果我尝试添加到 staticContent,它似乎不会改变标签。例如,这里是 web.config.transform 文件:

<configuration>
  <appSettings>
    <add key="WebPToolFolder" value ="~/Tools"/>
    <add key="ImagesFolder" value ="~/Content/themes/base/images"/>
  </appSettings>
<system.webServer>
    <staticContent>
      <mimeMap fileExtension=".webp" mimeType="image/webp" />
    </staticContent>
  </system.webServer>
</configuration>

它会更新 appSettings,但不会更新 staticContent 标签 - 有什么想法吗?

【问题讨论】:

  • 你找到解决这个问题的方法了吗?
  • 不,还没有运气!

标签: asp.net nuget nuspec


【解决方案1】:

老问题,但如果有人找到它,以下应该可以工作:

在您的情况下添加/更新 staticContent 元素:

这是一种替代解决方案,因此您不会使用 .transform 文件,而是使用我觉得更好的 web.config.install.xdt(和 web.config.uninstall.xdt):

<?xml version="1.0"?>
  <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <!-- some other elements -->
  <staticContent xdt:Transform="InsertIfMissing">
    <mimeMap fileExtension=".webp" mimeType="image/webp" xdt:Locator="Match(fileExtension)" xdt:Transform="InsertIfMissing" />
  </staticContent>
  <!-- some other elements -->
</configuration>

这样您就不需要做任何更新前的准备工作,只需升级软件包即可。

检查 this post 以获得从 Nuget 2.6 开始的 XDT 支持。

【讨论】:

    【解决方案2】:

    您需要在 web.config 中放置一个空的 &lt;staticContent&gt;&lt;/staticContent&gt;,然后在元素上使用 xdt:Transform="Insert",如下所示:

    您的 web.config:

    <configuration>
      <appSettings>
         <add key="WebPToolFolder" value ="~/Tools"/>
         <add key="ImagesFolder" value ="~/Content/themes/base/images"/>
      </appSettings>
      <system.webServer>
        <staticContent>
        </staticContent>
      <system.webServer>
    </configuration>
    

    然后你可以像这样在你的转换文件中插入一个值:

        <system.webServer>
        <staticContent>
            <mimeMap fileExtension=".webp" mimeType="image/webp" xdt:Transform="Insert"/>
        </staticContent>
    </system.webServer>
    

    我花了一段时间才知道。希望这会有所帮助。

    【讨论】:

      【解决方案3】:

      您是否尝试向要更新的标签添加 xdt:Transform="Replace" 属性?

      <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
        <appSettings>
          <add key="WebPToolFolder" value ="~/Tools" xdt:Transform="Replace"/>
          <add key="ImagesFolder" value ="~/Content/themes/base/images" xdt:Transform="Replace"/>
        </appSettings>
        <system.webServer>
          <staticContent>
            <mimeMap fileExtension=".webp" mimeType="image/webp" xdt:Transform="Replace"/>
          </staticContent>
        </system.webServer>
      </configuration>
      

      有一些很好的微软文档here

      如果您发布初始标记以及您希望它的外观,也许我们可以提供更多帮助 :)

      【讨论】:

        猜你喜欢
        • 2011-08-11
        • 1970-01-01
        • 1970-01-01
        • 2014-02-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多