如何发现.js,.css文件,文件系统(和repo)中的内容,但不在.csproj中?

时间:2018-02-21 09:01:31

标签: asp.net asp.net-mvc visual-studio deployment publish

上下文

我经常犯错误,忘记在Asp Mvc项目中包含供应商.css或.js。我只是使用工具或主题进行复制/下载,并引用它们。所有这些都在本地工作,因为文件位于虚拟目录中,因此IIS Express将为它们提供服务。

当发布时间到来并且我发布新版本时,将不会部署那些不在.csproj中的文件。

问题

虽然某些工具或IDE本身在某些情况下会在中创建警告,但如果在语法构造中我引用了.csproj中没有的资源,那么这不是全部工作(例如:使用BundleConfig) 这个错误来源似乎非常简单:只需使用精心挑选的过滤器检查文件系统,并列出.csproj中未包含的所有文件。 (过滤器可以是:(* .css, .js,...)或(assets / 。*)

我该如何完成这项任务?

1 个答案:

答案 0 :(得分:2)

如果切换到Visual Studio 2017支持的new .csproj format,则不再需要在文件系统中添加对文件的引用,默认情况下选中,您必须排除您不想要的文件。

迁移到新的.csproj格式非常简单 - 您可以使用dotnet migrate tool进行转换。

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFrameworks>net47</TargetFrameworks>
  </PropertyGroup>

  <ItemGroup>
    <ProjectReference Include="..\MyProj\MyProj.csproj" />
  </ItemGroup>

  <ItemGroup>

    <!-- /* Exclude files you don't want */ -->
    <Compile Remove="Text\AnyTransliterator.cs" />
    <Compile Remove="Text\BreakTransliterator.cs" />
  </ItemGroup>

</Project>

如果您希望包含项目目录的文件 ,则可以创建指向文件或目录的链接。

<!-- /* Link to an individual file outside of the project */ -->
<Content Include="..\..\..\Assets\something.css" Link="Assets\something.css" />

<!-- /* Create a virtual directory in Visual Studio named Assets
        and link to external Assets directory. All files in that
        directory will be included in the project */ -->
<Content Include="..\..\..\Assets\**\*" LinkBase="Assets" />

<!-- /* Create a virtual directory in Visual Studio named Assets
        and link to external Assets directory. Only .css files in that
        directory will be included in the project */ -->
<Content Include="..\..\..\Assets\**\*.css" LinkBase="Assets" />

适用于.NET Framework ,但请注意除了VS 2017 15.3之外还需要安装.NET Core SDK 2.0.0(并确保没有global.json选择可以使用LinkBase选项降低SDK版本。

参考: New .csproj format - How to specify entire directory as "linked file" to a subdirectory?