简体   繁体   English

对于.NET Core,Visual Studio会发布忽略Views文件夹中的.js文件

[英]Visual Studio publish ignores .js files in Views folder, for .NET Core

Here's the steps: 步骤如下:

  1. Create a simple project using ASP.NET Core Web Application template 使用ASP.NET Core Web应用程序模板创建一个简单的项目
  2. Publish it without modifying anything, to a folder 将其发布而不修改任何内容到文件夹

If Views folder is not in the published output, it's because your version of .NET Core compiles views by default. 如果Views文件夹不在发布的输出中,那是因为您的.NET Core版本默认情况下会编译视图。 Simply add this to your .csproj : 只需将其添加到您的.csproj

<PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
    <MvcRazorCompileOnPublish>false</MvcRazorCompileOnPublish>
  </PropertyGroup>

And publish again. 并再次发布。

  1. Now add a Home.js file to Views/Default folder 现在将Home.js文件添加到Views/Default文件夹
  2. Publish again to a folder 再次发布到文件夹
  3. As you can see, Home.cshtml is published to the output folder, but Home.js is not. 如您所见, Home.cshtml被发布到输出文件夹,但Home.js没有发布。

What's wrong here? 怎么了 This makes our deployment engine inefficient and we need to manually keep .js files in sync. 这使我们的部署引擎效率低下,我们需要手动保持.js文件同步。

Conventions are followed for a reason. 遵循约定是有原因的。 It promotes good app design. 它促进了良好的应用程序设计。 Having static files littered all over you project is not good app design . 在您的项目中到处乱堆的静态文件不是很好的应用程序设计 Still, you are correct that ASP.NET Core is configurable and can be made to do whatever you want to do with it, good or bad. 不过,您是正确的,因为ASP.NET Core是可配置的,并且可以使您执行它想做的任何事情,无论好坏。

You've actually got two issues here: 1) files in the Views directory are not included in the publish and 2) the Views directory is not served . 实际上,这里有两个问题:1) Views目录中的文件未包含在发布中; 2) Views目录未提供 Even if you are successful including the static files in the publish, it's a pyrrhic victory, as you won't actually be able to utilize those files in your web pages. 即使您成功地将静态文件包含在发布中,这也是一个巨大的胜利,因为您实际上将无法在网页中使用这些文件。

There's no way that I'm aware of to cause an entire directory to be copied to output via the UI - only individual files. 我知道没有办法导致整个目录通过UI复制到输出-仅单个文件。 However, by editing your project file, you can add something like: 但是,通过编辑项目文件,您可以添加如下内容:

<ItemGroup>
    <Content Include="Views\**\*.js" CopyToPublishDirectory="PreserveNewest" />
</ItemGroup>

That then just leaves you with the issue of actually serving these files, which becomes more problematic, as you cannot just serve some files, but only entire directories. 这样一来,您实际上就无法处理这些文件,而问题就变得更加棘手,因为您不能只提供某些文件,而只能提供整个目录。 That means potentially exposing anything in the Views directory. 这意味着潜在地暴露Views目录中的任何内容。 Still, if you insist: 不过,如果您坚持:

app.UseStaticFiles(); // For the wwwroot folder

app.UseStaticFiles(new StaticFileOptions
{
    FileProvider = new PhysicalFileProvider(
        Path.Combine(Directory.GetCurrentDirectory(), "Views")),
    RequestPath = "/Views"
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Visual Studio 2017发布asp.net核心 - visual studio 2017 publish asp.net core Visual Studio 中 .net 核心项目的锁定文件 - locked files of .net core project in Visual Studio .NET Core 在发布中包含文件夹 - .NET Core include folder in publish Asp.net 核心 3.0 在发布或构建时发布视图文件 (.cshtml) - Asp.net core 3.0 Publish Views files (.cshtml) on Publish or on Build 无法使用Visual Studio 2017将ASP.NET Core Angular项目发布到Azure - Failed to publish ASP.NET Core Angular Project to Azure with Visual Studio 2017 单击发布为Visual Studio 2017中的ASP.NET Core 2.0项目没有任何作用 - Clicking Publish for an ASP.NET Core 2.0 project in Visual Studio 2017 does nothing Visual Studio 2017使用C#7.2发布ASP.NET Core应用程序 - Visual Studio 2017 publish ASP.NET Core app with C# 7.2 每当 Visual Studio 构建特定的.Net Core 项目时,如何自动发布它? - How to publish the particular .Net Core project automatically whenever it is built by Visual Studio? 通过 Visual Studio FTP 将 ASP.NET Core MVC 网站发布到 IONOS 1&amp;1 - Publish ASP.NET Core MVC website to IONOS 1&1 via Visual Studio FTP .net core 2.0通过运行命令准备将所有文件发布到一个文件夹中 - .net core 2.0 prepare all files to publish in one folder in by running command
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM