确保所有* .cshtml文件被设置为"内容及QUOT;用于生成操作 [英] Make sure all *.cshtml files are set to be "Content" for Build Action

查看:370
本文介绍了确保所有* .cshtml文件被设置为"内容及QUOT;用于生成操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有几次,当我复制粘贴 *。CSHTML 文件时,Visual Studio出于某种原因套生成操作这些文件是无:

A few times when I copy-paste *.cshtml files, Visual Studio for some reason sets Build Action on these files to be "None":

这是不可能的检测,当你在当地工作,因为这些文件是present。但是,当你部署通过WebDeploy,标记文件作为生成操作无不是包装。其结果是我得到的服务器上非工作的应用程序。

This is impossible to detect when you work locally, because the files are present. But when you deploy through WebDeploy, files marked as "None" on Build Action are not packaged. As a result I get non-working application on the server.

问:是有办法来自动检测这种事件和prevent

推荐答案

您可以用一个小片段,它会生成一个警告延长 .csproj的时,在一个项目无群体的扩展 .cshtml 。该片段是:

You could extend the .csproj with a small snippet that will generate a warning when an item in the "None" group has the extension .cshtml. The snippet would be:

<Target Name="EnsureContentOnViews" BeforeTargets="BeforeBuild">
  <ItemGroup>
    <Filtered Include="@(None)" Condition="'%(Extension)' == '.cshtml'" />
  </ItemGroup>
  <Warning 
    Condition="'@(Filtered)'!=''"
    Code="CSHTML" 
    File="$(MSBuildProjectDirectory)\%(Filtered.Identity)" 
    Text="View is not set to [BuildAction:Content]"
  />
</Target>

如果你看其他版本的操作(如 EmbeddedResource ),你可以将它们添加到过滤项定义。

If you see other build actions (like EmbeddedResource), you can add them to the Filtered item definition.

如果您想了解更多先进的检测,你需要实际解析项目文件适合这个XPath //的ItemGroup / * [不(个体经营::内容)] / @包含

If you want more advanced detection you need to actually parse the project files for any item that fits this Xpath //ItemGroup/*[not(self::Content)]/@Include

<Target Name="EnsureContentOnViewsXML" BeforeTargets="BeforeBuild">
  <XmlPeek XmlInputPath="$(MSBuildProjectFile)" Namespaces="&lt;Namespace Prefix='msb' Uri='schemas.microsoft.com/developer/msbuild/2003'/&gt;"; Query="/msb:Project/msb:ItemGroup/*[not(self::msb:EmbeddedResource)]/@Include">
    <Output TaskParameter="Result" ItemName="AllItems" />
  </XmlPeek>

  <!-- MsBuild uses XPath 1.0 which doesn't have the 'ends-with' or 'matches' function. -->
  <ItemGroup>
    <Filtered Include="@(AllItems)" Condition="'%(Extension)' == '.cshtml'" />
  </ItemGroup>

  <Warning 
    Code="CSHTML" 
    File="$(MSBuildProjectDirectory)\%(Filtered.Identity)" 
    Text="View is not set to [BuildAction:Content]"
    Condition="'@(Filtered)'!=''"
  />
</Target>

而不是&LT的;警告...&GT; 你也可以使用&LT;错误...&GT;

您需要手动把这些片段中的一个项目文件:

You'll need to manually put one of these snippets in your project file:

这篇关于确保所有* .cshtml文件被设置为&QUOT;内容及QUOT;用于生成操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆