发布是不是在改变web.config?

Bru*_*oLM 15 asp.net deployment web-config .net-4.0 web-config-transform

我做了一个web.config(完整文件,它没有显示XML错误)

<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
  <configSections>
      ...
      <location path="." inheritInChildApplications="false">
        <connectionStrings>
          <add name="ElmahLog" connectionString="data source=~/App_Data/Error.db" />
          <add name="database" connectionString="w" providerName="System.Data.EntityClient"/>
        </connectionStrings>
      </location>
  ...
Run Code Online (Sandbox Code Playgroud)

使用转换文件(web.Staging.config)

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <connectionStrings>
    <add name="database"
      connectionString="c"
      providerName="System.Data.EntityClient"
      xdt:Transform="SetAttributes" xdt:Locator="Match(name)" />
  </connectionStrings>
  <system.web>
    <compilation xdt:Transform="RemoveAttributes(debug)" />
    <customErrors defaultRedirect="error.aspx"
      mode="RemoteOnly" xdt:Transform="Replace">
    </customErrors>
  </system.web>
</configuration>
Run Code Online (Sandbox Code Playgroud)

我在Staging模式下发布(右键单击网站>发布>方法:文件系统...)

------ Build started: Project: Drawing, Configuration: Staging Any CPU ------
  Drawing -> D:\Project\bin\Staging\Drawing.dll
------ Build started: Project: MySystem, Configuration: Staging Any CPU ------
  MySystem -> D:\Project\bin\Staging\MySystem.dll
...
Run Code Online (Sandbox Code Playgroud)

但是,当我web.config在输出文件夹中查看它时,它没有被更改.

我在Build日志中找到了以下内容:

D:\Project\Web.Staging.config(3,2): Warning : No element in the source document matches '/configuration'
D:\Project\Web.Staging.config(3,2): Warning : No element in the source document matches '/configuration'
D:\Project\Web.Staging.config(3,2): Warning : No element in the source document matches '/configuration'
Transformed web.config using Web.Staging.config into obj\Staging\TransformWebConfig\transformed\web.config.
Run Code Online (Sandbox Code Playgroud)

可能是什么问题呢?我这样做了吗?

小智 22

回答迟到但也许我可以让别人头疼.在Visual Studio 2013中,有两个位置可供选择用于构建和部署的配置.配置管理器,然后再次使用"发布Web",其中"向导"向导中的第三步允许您选择要使用的配置.如果您没有选择新配置,它将使用所选配置的转换而不是您的配置.

  • 你确实让我头疼. (5认同)
  • 这是真正的答案。 (2认同)

Bru*_*oLM 17

我发现了两件事:

  • 您无法在<configuration>标记上设置命名空间(例如:for <location path="." inheritInChildApplications="false">)
  • 您必须在转换文件中查看正确的层次结构.

喜欢

<configuration>
  <location>
    <connectionStrings>
Run Code Online (Sandbox Code Playgroud)

代替

<configuration>
  <connectionStrings>
Run Code Online (Sandbox Code Playgroud)


Aar*_*nus 5

确保在Web.Config文件的属性中将Build Action其设置为Content

如果将构建操作设置为None,即使将其复制到输出目录,也不会对其进行转换。