allowDefinition ='MachineToApplication'错误设置<MvcBuildViews> true </ MvcBuildViews>

Dar*_*one 6 msbuild asp.net-mvc visual-studio-2012

在我的Asp.Net MVC 4项目中,我已经在.csproj文件中设置了构建视图<MvcBuildViews>true</MvcBuildViews>.问题是构建项目我得到了错误:

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>
Run Code Online (Sandbox Code Playgroud)

通常,我能够通过运行应用程序(我收到错误)来运行应用程序,构建应用程序并在此之后再次运行.

new*_*nth 9

执行@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.

那么是什么导致这个问题?当项目发布时,编译器默认使用它<project-dir>\obj\来放置它将使用的源文件的副本.遗憾的是,发布完成后,这些文件不会自动删除.下次开发人员编译项目时MvcBuildViews=true,它会出错,因为aspnet编译器obj\在编译期间包含文件夹,因为它位于<project-dir>文件夹下面.

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

  1. 设置MvcBuildViews=false.我并不认为这是一个解决方案,所以让我们继续前进.
  2. 删除中的文件<project-dir>\obj\.工作,但可能是一个麻烦,因为它必须在每次发布后完成.
  3. 通过使用<BaseIntermediateOutputPath>项目配置文件中的属性,将发布用作中间目录的路径更改为.

    示例(参考:此链接):

    <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>
    
    Run Code Online (Sandbox Code Playgroud)

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

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


C. *_*alt 0

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

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

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

尝试将其注释掉,这样编译器的行为就不会改变

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