【问题标题】:allowDefinition='MachineToApplication' error setting <MvcBuildViews>true</MvcBuildViews>allowDefinition='MachineToApplication' 错误设置 <MvcBuildViews>true</MvcBuildViews>
【发布时间】:2012-10-08 08:41:07
【问题描述】:

在我的 Asp.Net MVC 4 项目中,我已在 .csproj 文件中设置以构建视图 &lt;MvcBuildViews&gt;true&lt;/MvcBuildViews&gt;。问题是构建项目我得到了错误:

It is an error to use a section registered as allowDefinition='MachineToApplication' beyond application level. This error can be caused by a virtual directory not being configured as an application in IIS.

我试图删除 obj 文件夹,但错误不断出现。错误表明问题出在身份验证标记行中:

<system.web>
    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login" timeout="2880" />
    </authentication>

通常,我可以通过运行应用程序(我收到错误)、构建应用程序然后再次运行来运行应用程序。

【问题讨论】:

标签: asp.net-mvc msbuild visual-studio-2012


【解决方案1】:

按照@matrixugly 的建议进行操作将解决问题,但也会导致编译时视图检查停止工作。我假设您仍然想在编译时错误检查您的视图?如果是这种情况,请在下面进行更好的修复。

为了理解这些解决方案为什么有效,我们首先要知道问题是如何产生的:

  1. 开发人员希望在编译时检查视图,因此他们设置了MvcBuildViews=true
  2. 应用程序构建良好,直到他们发布项目。
  3. 随后尝试构建项目会导致编译时错误:It is an error to use a section registered as allowDefinition='MachineToApplication' beyond application level. This error can be caused by a virtual directory not being configured as an application in IIS.

那么导致这个问题的原因是什么?当项目发布时,编译器默认使用&lt;project-dir&gt;\obj\ 来放置它将使用的源文件的副本。不幸的是,这些文件不会在发布完成后自动删除。下次开发者用MvcBuildViews=true编译项目时会报错,因为aspnet编译器在编译过程中包含了obj\文件夹,因为它在&lt;project-dir&gt;文件夹下面。

那么我们该如何解决这个问题呢?好吧,你有四个选择:

  1. 设置MvcBuildViews=false。我真的不认为这是一个解决方案,所以让我们继续吧。
  2. 删除&lt;project-dir&gt;\obj\ 中的文件。有效,但可能会很麻烦,因为必须在每次发布后完成。
  3. 通过在项目配置文件中使用 &lt;BaseIntermediateOutputPath&gt; 属性来更改发布用作中间目录的路径。

    示例(参考:this link):

    <BaseIntermediateOutputPath> [SomeKnownLocationIHaveAccessTo] </BaseIntermediateOutputPath>

  4. 在您的项目配置文件中添加一个新部分,该部分会在构建时为您删除有问题的文件(参考 Microsoft Connect)。我什至为您简化了操作,只需复制和粘贴即可:

    <PropertyGroup>
    <_EnableCleanOnBuildForMvcViews Condition=" '$(_EnableCleanOnBuildForMvcViews)'=='' ">true</_EnableCleanOnBuildForMvcViews>
    </PropertyGroup>
    <Target Name="CleanupForBuildMvcViews" Condition=" '$(_EnableCleanOnBuildForMvcViews)'=='true' and '$(MVCBuildViews)'=='true' " BeforeTargets="MvcBuildViews">
    <ItemGroup>
        <_TempWebConfigToDelete Include="$(BaseIntermediateOutputPath)**\Package\**\*" />
        <_TempWebConfigToDelete Include="$(BaseIntermediateOutputPath)**\TransformWebConfig\**\*" />
        <_TempWebConfigToDelete Include="$(BaseIntermediateOutputPath)**\CSAutoParameterize\**\*" />
        <_TempWebConfigToDelete Include="$(BaseIntermediateOutputPath)**\TempPE\**\*" />
    </ItemGroup>
    
    <Delete Files="@(_TempWebConfigToDelete)"/>
    </Target>
    

我的建议是使用选项 3 或 4。

注意对于那些从未编辑过他们的项目文件的人,您无法在加载时对其进行编辑。必须首先通过右键单击它并选择Unload Project 来卸载它。然后,您可以右键单击项目并编辑项目文件。或者,您可以在 Visual Studio 之外编辑该文件。

【讨论】:

  • 太棒了,很好的解释!
【解决方案2】:

在启用 MvcBuildViews 验证我的 Razor 语法后尝试发布我的 Web 应用程序时,我遇到了完全相同的问题

我在我的网络配置中找到了这段代码

  <Target Name="MvcBuildViews" AfterTargets="AfterBuild" Condition="'$(MvcBuildViews)'=='true'">
    <AspNetCompiler VirtualPath="temp" PhysicalPath="$(WebProjectOutputDir)" />
  </Target>

试着把它注释掉,这样编译器的行为就不会改变

  <!--<Target Name="MvcBuildViews" AfterTargets="AfterBuild" Condition="'$(MvcBuildViews)'=='true'">
<AspNetCompiler VirtualPath="temp" PhysicalPath="$(WebProjectOutputDir)" />
</Target>-->

【讨论】:

  • 如果您要对一个将近 4 年的答案投反对票,请发表评论,谢谢 ;)
  • 这并不能解决问题。但是如果你想禁用MvcBuildViews,那么在你的.csproj中,在PropertyGroup中,添加这个:&lt;MvcBuildViews&gt;False&lt;/MvcBuildViews&gt;(或者也许.pubxml发布配置文件会覆盖它,然后将它设置在那里)你没有找到这个您的 web.config,但文件系统上的一些 .target 文件可能会被一些 msbuild 更新覆盖。
【解决方案3】:

@newmanth 的答案非常好,但已经过时了。 2022 年,让我告诉你——这个CleanupForBuildMvcViews 实际上正式包含在C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\MSBuild\Microsoft\VisualStudio\v16.0\Web\Microsoft.Web.Publishing.targets 中! :)

它甚至将指向(现已损坏)Microsoft Connect 的链接作为@newmanth 引用提供。

这里是sn-p:

  <!--Deal with http://connect.microsoft.com/VisualStudio/feedback/details/779737/error-allowdefinition-machinetoapplication-beyond-application-level, 
  we will need to clean up our temp folder before MVC project starts the pre-compile-->
  <PropertyGroup>
    <_EnableCleanOnBuildForMvcViews Condition=" '$(_EnableCleanOnBuildForMvcViews)'=='' ">true</_EnableCleanOnBuildForMvcViews>
  </PropertyGroup>
  <Target Name="CleanupForBuildMvcViews" Condition=" '$(_EnableCleanOnBuildForMvcViews)'=='true' and '$(MVCBuildViews)'=='true' " BeforeTargets="MvcBuildViews">
    <ItemGroup>
      <_PublishTempFolderNamesToCleanup Include="Database;TransformWebConfig;CSAutoParameterize;InsertAdditionalCS;ProfileTransformWebConfig;Package" />
    </ItemGroup>
    <!--Force msbuild to expand all the wildcard characters so to get real file paths-->
    <CreateItem Include="@(_PublishTempFolderNamesToCleanup->'$(BaseIntermediateOutputPath)**\%(identity)\**\*')">
      <Output TaskParameter="Include" ItemName="_EvaluatedPublishTempFolderNamesToCleanup" />
    </CreateItem>
    <Delete Files="@(_EvaluatedPublishTempFolderNamesToCleanup)" />
  </Target>

但是我仍然得到上述异常。在我的情况下,我也必须删除 AspnetCompileMerge 文件夹。并用另一个名字命名 Target,不要覆盖它:

  <PropertyGroup>
    <_EnableCleanOnBuildForMvcViews Condition=" '$(_EnableCleanOnBuildForMvcViews)'=='' ">true</_EnableCleanOnBuildForMvcViews>
  </PropertyGroup>
  <Target Name="CleanupForBuildMvcViews2" Condition=" '$(_EnableCleanOnBuildForMvcViews)'=='true' and '$(MVCBuildViews)'=='true' " BeforeTargets="MvcBuildViews">
    <ItemGroup>
        <_TempWebConfigToDelete Include="$(BaseIntermediateOutputPath)**\AspnetCompileMerge\**\*" />
    </ItemGroup>
    <Delete Files="@(_TempWebConfigToDelete)"/>
  </Target>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-13
    • 2011-02-15
    • 1970-01-01
    • 2012-05-23
    相关资源
    最近更新 更多