Web.config转换:无法识别的属性'xmlns:xdt'.请注意,属性名称区分大小写

And*_*rry 44 web-config asp.net-mvc-3

我在MVC 3.0项目中遇到这个奇怪的间歇性错误当我构建项目时,有时会收到以下错误消息:

无法识别的属性'xmlns:xdt'.请注意,属性名称区分大小写.

这是指标准的web.config转换文件(下面复制的Web.Release.config)没有其他错误或警告.这是在调试模式和发布中发生的.如果我清理解决方案,有时它会清除

开始更新

发现了这个问题.在MVC项目文件(MyProject.csproj)中,我将构建视图设置为true

<MvcBuildViews>true</MvcBuildViews>
Run Code Online (Sandbox Code Playgroud)

一旦回到,上面的错误消失了.我想建立视图,因为它停止了很多愚蠢的视图代码错误等,并且是性能增强(页面是预编译而不是jit)

有谁知道这是什么导致错误?这是一个错误吗?

结束更新

<?xml version="1.0"?>

<!-- For more information on using Web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <!--
    In the example below, the "SetAttributes" transform will change the value of 
    "connectionString" to use "ReleaseSQLServer" only when the "Match" locator 
    finds an atrribute "name" that has a value of "MyDB".

    <connectionStrings>
      <add name="MyDB" 
        connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True" 
        xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
    </connectionStrings>
  -->
  <system.web>
    <compilation xdt:Transform="RemoveAttributes(debug)" />
    <!--
      In the example below, the "Replace" transform will replace the entire 
      <customErrors> section of your Web.config file.
      Note that because there is only one customErrors section under the 
      <system.web> node, there is no need to use the "xdt:Locator" attribute.

      <customErrors defaultRedirect="GenericError.htm"
        mode="RemoteOnly" xdt:Transform="Replace">
        <error statusCode="500" redirect="InternalError.htm"/>
      </customErrors>
    -->
  </system.web>
</configuration>
Run Code Online (Sandbox Code Playgroud)

小智 78

我遇到了同样的问题.你会发现很多与MvcBuildViews有关的戏弄和各种错误条件.但似乎没有人提到这个特殊的错误.一个对我有用的快速修复是删除受影响的Web项目的"obj"目录的内容,然后重建.

  • 男孩,我认为'清洁解决方案'就是这个意思.猜猜我错了. (7认同)
  • 在 VS 2019 中对我不起作用。我运行了 Clean 解决方案,我删除了 obj 和 bin,然后运行了 Rebuild All。 (2认同)

And*_*lil 31

这是一种解决方法,但您可以在预构建命令中添加以下行:

del $(ProjectDir)obj\* /F /S /Q

右键单击项目>"属性">"构建事件">"预构建"

  • 如果您的项目位于其中包含空格的路径中,请记住在$(ProjectDir)obj\*`部分周围包装引号. (4认同)

Fly*_*ynn 10

这适用于Continuous Integration和WebDeploy:

我设置的那一刻就会出现此问题

<MvcBuildViews>true</MvcBuildViews>
Run Code Online (Sandbox Code Playgroud)

在我的项目文件中,我需要做.

在阅读并测试了我发现的关于这个问题的所有内容后,我有一个wokraround,也可以通过MSBuild与WebDeploy一起使用

MSBUild.exe ... /p:DeployOnBuild=true
Run Code Online (Sandbox Code Playgroud)

您(仅)需要在构建前和构建后事件期间删​​除构建文件夹中的TransformWebConfig子文件夹.它甚至适用于连续集成服务器,如果没有文件夹则会中断

预构建事件命令行:

if exist "$(ProjectDir)obj\$(ConfigurationName)\transformwebconfig\" del "$(ProjectDir)obj\$(ConfigurationName)\transformwebconfig\*" /F /S /Q
Run Code Online (Sandbox Code Playgroud)

构建后事件命令行:

if exist "$(ProjectDir)obj\$(ConfigurationName)\transformwebconfig\" del "$(ProjectDir)obj\$(ConfigurationName)\transformwebconfig\*" /F /S /Q
Run Code Online (Sandbox Code Playgroud)

Resharper如果删除整个obj文件夹,这甚至可以正常工作.

一定要设置Run the post-build eventalways!!

更新: 用$(ConfigurationName)替换调试和发布,并删除生成的重复行

  • 此处创建的连接问题:https://connect.microsoft.com/VisualStudio/feedback/details/933625/setting-mvcbuildviews-true-causes-error-unrecognized-attribute-xmlns-xdt-note-that-attribute-names-are - 案例敏感#标签 (2认同)