asp.net-mvc 从 VS2010 发布时出现 allowDefinition='MachineToApplication' 错误(但仅在之前的构建之后)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2566215/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:14:28  来源:igfitidea点击:

allowDefinition='MachineToApplication' error when publishing from VS2010 (but only after a previous build)

asp.net-mvcweb-deploymentone-click-web-publishing

提问by Dan

I can run my Asp.Net MVC 2 application without an issue on my local computer. Just Run / Debug.

我可以在本地计算机上运行我的 Asp.Net MVC 2 应用程序而不会出现问题。只需运行/调试。

But if I have already built it, I can't publish it! I have to clean the solution and publish it again. I know this is not system critical, but it's really annoying. "One Click Publish" is not "Clean solution and then One click publish"

但是如果我已经建立了它,我不能发布它!我必须清理解决方案并再次发布。我知道这不是系统关键,但它真的很烦人。“一键发布”不是“干净的解决方案,然后一键发布”

The exact error is as follows:

确切的错误如下:

Error 11 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.

错误 11 在应用程序级别之外使用注册为 allowDefinition='MachineToApplication' 的部分是错误的。此错误可能是由于虚拟目录未配置为 IIS 中的应用程序所致。

I suspect it's something to do with the Web.Config in the Views folder, but then why only after I build once previously. And just to note, the app works fine once published.

我怀疑这与 Views 文件夹中的 Web.Config 有关,但为什么只有在我之前构建一次之后。需要注意的是,该应用程序发布后运行良好。

采纳答案by benpage

i had the same problem with my MVC apps. it was frustrating because i still wanted my views to be checked, so i didn't want to turn off MvcBuildViews

我的 MVC 应用程序也有同样的问题。这很令人沮丧,因为我仍然希望检查我的视图,所以我不想关闭MvcBuildViews

luckily i came across a postwhich gave me the answer. keep the MvcBuildViews as true, then you can add the following line underneath in your project file:

幸运的是,我遇到了一个帖子,它给了我答案。保持 MvcBuildViews 为true,然后您可以在项目文件下面添加以下行:

<BaseIntermediateOutputPath>[SomeKnownLocationIHaveAccessTo]</BaseIntermediateOutputPath>

And make that folder not in your project's folder. Works for me. It's not a perfect solution, but it's good for the moment. Make sure you remove the packagefolder (located inside the obj\Debugand/or obj\Releasefolder) from your project folder otherwise you'll keep getting the error.

并使该文件夹不在您的项目文件夹中。对我来说有效。这不是一个完美的解决方案,但暂时是好的。确保从项目文件夹中删除文件夹(位于obj\Debug和/或obj\Release文件夹内),否则您将不断收到错误消息。

FWIW, MS know about this error...

FWIW,MS 知道这个错误......

回答by jimmay5469

I deleted everything out of my obj/Debug folder and it fixed this error. This allowed me to leave in the

我从 obj/Debug 文件夹中删除了所有内容,并修复了此错误。这让我得以离开

<MvcBuildViews>true</MvcBuildViews>

option in my project file (which comes in handy with the T4MVC T4 template).

我的项目文件中的选项(它与 T4MVC T4 模板一起使用)。

Edit: This can be achieved much easier by simply using the "Build" -> "Rebuild Solution" menu (because what rebuild actually does is clear the obj/Debug folder and then build solution).

编辑:这可以通过简单地使用“构建”->“重建解决方案”菜单来轻松实现(因为重建实际上是清除 obj/Debug 文件夹,然后构建解决方案)。

回答by jrummell

I'm using this workaround on the MS Connectpage for this error. It cleans all obj and temp files under your project (all configurations) before running AspNetCompiler.

我正在MS Connect页面上使用此解决方法来解决此错误。在运行 AspNetCompiler 之前,它会清除您的项目(所有配置)下的所有 obj 和临时文件。

Modify the MvcBuildViews target in your project file so that it depends on the targets that clean up the packaging files that Visual Studio has created. These targets are included in web application projects automatically.

All packaging files will be deleted every time that the MvcBuildViews target executes.

修改项目文件中的 MvcBuildViews 目标,使其依赖于清理 Visual Studio 创建的打包文件的目标。这些目标自动包含在 Web 应用程序项目中。

每次执行 MvcBuildViews 目标时,所有打包文件都会被删除。

<Target Name="MvcBuildViews" AfterTargets="AfterBuild" Condition="'$(MvcBuildViews)'=='true'" DependsOnTargets="CleanWebsitesPackage;CleanWebsitesPackageTempDir;CleanWebsitesTransformParametersFiles;">
    <AspNetCompiler VirtualPath="temp" PhysicalPath="$(MSBuildProjectDirectory)" />
</Target>

回答by Chris Hynes

This problem occurs when there is web project output (templated web.config or temporary publish files) in the obj folder. The ASP.NET compiler used isn't smart enough to ignore stuff in the obj folder, so it throws errors instead.

当 obj 文件夹中有 web 项目输出(模板化 web.config 或临时发布文件)时,会出现此问题。所使用的 ASP.NET 编译器不够智能,无法忽略 obj 文件夹中的内容,因此会引发错误。

Another fix is to nuke the publish output right before calling <AspNetCompiler>. Open your .csproj and change this:

另一个解决方法是在调用 <AspNetCompiler> 之前立即取消发布输出。打开您的 .csproj 并更改它:

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

to this:

对此:

<Target Name="MvcBuildViews" AfterTargets="AfterBuild" Condition="'$(MvcBuildViews)'=='true'">
  <ItemGroup>
    <ExtraWebConfigs Include="$(BaseIntermediateOutputPath)\**\web.config" />
    <ExtraPackageTmp Include="$([System.IO.Directory]::GetDirectories(&quot;$(BaseIntermediateOutputPath)&quot;, &quot;PackageTmp&quot;, System.IO.SearchOption.AllDirectories))" />
  </ItemGroup>
  <Delete Files="@(ExtraWebConfigs)" />
  <RemoveDir Directories="@(ExtraPackageTmp)" />
  <AspNetCompiler VirtualPath="temp" PhysicalPath="$(WebProjectOutputDir)" />
</Target>

That will delete all web.configs under \obj, as well as all PackageTmp folders under \obj.

这将删除 \obj 下的所有 web.configs,以及 \obj 下的所有 PackageTmp 文件夹。

回答by Richard Szalay

If you are using Web Publish, you can set MvcBuildViews=falseand PrecompileBeforePublish=true, which precompiles after the copy to the temporary folder (immediately before publish/package).

如果您使用的是 Web 发布,则可以设置MvcBuildViews=falsePrecompileBeforePublish=true,它会在复制到临时文件夹后(紧接在发布/打包之前)进行预编译。

NOTE: PrecompileBeforePublishis only supported by the "new" Web Publishing Pipeline stack (VS2010 SP1 + Azure SDK or VS2012 RTM). If you're using VS2010 RTM, you'll need use one of the alternative methods.

注意:PrecompileBeforePublish仅受“新”Web 发布管道堆栈(VS2010 SP1 + Azure SDK 或 VS2012 RTM)支持。如果您使用的是 VS2010 RTM,则需要使用其中一种替代方法。

回答by nootn

I know this has been answered but I just wanted to add something interesting I found.

我知道这已经得到了回答,但我只是想添加一些我发现的有趣内容。

I had set the "MvcBuildViews" to false in the project, deleted all bin and obj folders and I was still getting the error. I found that there was a ".csproj.user" file that still had "MvcBuildViews" set to true.

我在项目中将“MvcBuildViews”设置为 false,删除了所有 bin 和 obj 文件夹,但仍然出现错误。我发现有一个“.csproj.user”文件仍然将“MvcBuildViews”设置为true。

I deleted the ".csproj.user" file and then it all worked.

我删除了“.csproj.user”文件,然后一切正常。

So make sure if you are changing your csproj file that you either change or delete the ".csproj.user" file also.

因此,请确保您在更改 csproj 文件时也更改或删除了“.csproj.user”文件。

回答by Pawel Gorczynski

Regarding the solution by jrummell, the setting:

关于jrummell的解决方案,设置:

DependsOnTargets="CleanWebsitesPackage;CleanWebsitesPackageTempDir;CleanWebsitesTransformParametersFiles;"

DependsOnTargets="CleanWebsitesPackage;CleanWebsitesPackageTempDir;CleanWebsitesTransformParametersFiles;"

It works in VS 2010, but not in VS 2012. In 2012 you have to put:

适用于 VS 2010,但不适用于 VS 2012。在 2012 年你必须把:

DependsOnTargets="CleanWebsitesPackage;CleanWebsitesWPPAllFilesInSingleFolder;CleanWebPublishPipelineIntermediateOutput"

DependsOnTargets="CleanWebsitesPackage;CleanWebsitesWPPAllFilesInSingleFolder;CleanWebPublishPipelineIntermediateOutput"

Source:

来源:

VS 2010: C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.targets

VS 2010:C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.targets

VS 2012: C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v11.0\Web\Microsoft.Web.Publishing.targets

VS 2012:C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v11.0\Web\Microsoft.Web.Publishing.targets

回答by eppdog

I had this problem as well, so I created a Pre-Build Event in the project properties to Clean the output directories(${projectPath}\bin,${projectPath}\obj\${ConfigurationName}). On another project I was also getting this error, even with the cleaning event in place. On the second project I was compiling the views as listed in the project file:

我也有这个问题,所以我在项目属性中创建了一个预构建事件来清理输出目录(${projectPath}\bin,${projectPath}\obj\${ConfigurationName})。在另一个项目中,即使进行了清洁活动,我也遇到了此错误。在第二个项目中,我正在编译项目文件中列出的视图:

<MvcBuildViews>true</MvcBuildViews>

I changed the true to false, and it no longer complained about that error, but still ran correctly. I won't claim I know exactly what was causing the second error, but at least it got me moving forward for the time being.

我将 true 更改为 false,它不再抱怨该错误,但仍然正确运行。我不会声称我确切地知道是什么导致了第二个错误,但至少它让我暂时向前迈进了一步。

回答by JotaBe

The problem has to do with the intermediate files, but there is another solution which consist in cleaning up those intermediate files before builnding the views.

问题与中间文件有关,但还有另一种解决方案,即在构建视图之前清理这些中间文件。

This solution has been included in some version of VS, but I can only say that I had the problem in VS 2013 Update 5. (See the "Beware"below, it could be fixed in this version, but not working only in my particular non-standard case).

此解决方案已包含在某些版本的 VS 中,但我只能说我在 VS 2013 Update 5 中遇到了问题。(请参阅下面的“注意”,它可以在此版本中修复,但仅在我的特定版本中不起作用非标准情况)。

I borrowed the soltuion from Error: allowDefinition='MachineToApplication' beyond application level on Visual Studio Connect.

我从Visual Studio Connect 上的应用程序级别之外的 Error: allowDefinition='MachineToApplication' 中借用了解决方案

The solution consist in including these lines to the web application project (.csprojfile) which handle the deletion of the offedning intermediate files:

解决方案包括将这些行包含到.csproj处理有问题的中间文件的删除的 Web 应用程序项目(文件)中:

<!--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;AspnetCompileMerge" />
    </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>

Beware:for some reason, probably because I included it myself in the project, my build target for building the views was named "BuildViews", instead of "MvcBuildViews", so I had to modify the BeforeTargetsattribute accordingly. I also simplified the target, by removing the PropertyGroupand simplifying the condition, like this:

注意:出于某种原因,可能是因为我自己将它包含在项目中,我用于构建视图的构建目标被命名为"BuildViews",而不是"MvcBuildViews",因此我必须相应地修改BeforeTargets属性。我还通过删除PropertyGroup和简化条件来简化目标,如下所示:

  <Target Name="CleanupForBuildMvcViews" Condition="'$(MVCBuildViews)'=='true' " BeforeTargets="BuildViews">
    <ItemGroup>
     <_PublishTempFolderNamesToCleanup Include="Database;TransformWebConfig;CSAutoParameterize;InsertAdditionalCS;ProfileTransformWebConfig;Package;AspnetCompileMerge" />
    </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>

回答by MoXplod

In my case i saw that when i have MvcBuildViews and PrecompileDuringPublish as both true - was what was causing this issue.

在我的情况下,我看到当我将 MvcBuildViews 和 PrecompileDuringPublish 都设为 true 时 - 是什么导致了这个问题。

So i removed the PrecompileDuringPublish and that solution worked for me and i have not faced this problem since.

所以我删除了 PrecompileDuringPublish 并且该解决方案对我有用,从那时起我就没有遇到过这个问题。

enter image description here

在此处输入图片说明