剃刀类库(RCL)中具有静态内容的404 [英] 404 with static content in Razor Class Library (RCL)

查看:35
本文介绍了剃刀类库(RCL)中具有静态内容的404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难在ASP.NET Core(v3.1)应用程序使用的剃刀类库(.net Core 3.1)中实现静态文件.

I am having difficulties implementing static files in razor class library (.net Core 3.1) that are consumed by an ASP.NET Core (v3.1) application.

当尝试访问静态文件时,我只会收到404-找不到.

When trying to access the static files, I only get an 404 - Not Found.

我在Stackoverflow上遵循以下答案: https://stackoverflow.com/a/57874357/1099519

I followed the following answer on Stackoverflow: https://stackoverflow.com/a/57874357/1099519 or

我还在以下位置对文档进行了交叉检查:

I also cross-checked with the documentation at: https://docs.microsoft.com/en-us/aspnet/core/razor-pages/ui-class?view=aspnetcore-3.0&tabs=visual-studio#create-an-rcl-with-static-assets.

我在库中的以下位置放置了一个CSS文件: wwwroot \ css \ Base.css ,我测试了以下路径:

I placed a css file in the library at the following location: wwwroot\css\Base.css and I tested the following path: https://localhost:44300/_content/OurIt.Cockpit/css/Base.css, which results in a 404 Not found response.

我已经检查过的内容:

  • 任何具有RCL内的视图的控制器都可以浏览并且可以正常工作(将呈现的HTML代码提供给浏览器)
  • 在Web应用程序中具有 app.UseStaticFiles(); .
  • 在Web应用程序中具有 webBuilder.UseStaticWebAssets(); .
  • 正确的外壳.
  • wwwroot \ css \ Base.css Build Action (构建操作)设置为 Content (内容).
  • RCL项目定义了以下sdk:< Project Sdk ="Microsoft.NET.Sdk.Razor"> .
  • 在RCL项目中设置了以下属性:

  • Any Controller with its Views inside the RCL can be browsed and they work fine (rendered HTML code is served to the browser)
  • Having app.UseStaticFiles(); in the web application.
  • Having webBuilder.UseStaticWebAssets(); in the web application.
  • Correct Casing.
  • The Build Action of wwwroot\css\Base.css is set to Content.
  • The RCL project has the following sdk defined: <Project Sdk="Microsoft.NET.Sdk.Razor">.
  • The following properties are set in the RCL project:

< PropertyGroup>< TargetFramework> netcoreapp3.1</TargetFramework>< AddRazorSupportForMvc> true</AddRazorSupportForMvc>< PropertyGroup>

我还尝试了此答案中所述的方法: https://stackoverflow.com/a/59574845/1099519 我尝试从 https://localhost:44300/path/css/Base访问文件.css

I also tried the way described in this answer: https://stackoverflow.com/a/59574845/1099519 and I tried to access the file from https://localhost:44300/path/css/Base.css

是否有机会调试或定位问题?维护Microsoft文档:

Is there any chance to debug or locate the issue? Reguarding to the Microsoft documentation:

构建RCL后,将生成一个清单,该清单描述了静态Web资产的位置.消费应用程序在运行时读取清单以消费引用的项目和程序包中的资产.

When the RCL is built, a manifest is produced that describes the static web asset locations. The consuming app reads the manifest at runtime to consume the assets from referenced projects and packages.

为了验证文件是否在程序集中,我试图找到该清单,但是找不到或不知道在哪里寻找(我检查了输出文件夹).我还尝试用ILSpy打开RCL,希望找到解决该问题的方法.

In order to verify that the files are in the assembly, I was trying to locate that manifrest, but I couldn't find it or don't know where to look for it (i checked the output folder). I also tried opend the RCL with ILSpy hoping to find a glue for the issue.

有什么想法(或使用带有.NET Core 3.1静态内容的RCL的示例-我只找到了控制器/视图的示例,但没有静态内容的示例)?

Any ideas (or working samples with a RCL with static content for .NET Core 3.1 - I only found samples for Controllers / Views but not with static content)?

我在Github上创建了一个样本进行复制: https://github.com/DominikAmon/RclIssueDemo

I created a sample on Github for reproduction: https://github.com/DominikAmon/RclIssueDemo

推荐答案

如果从RclDemo.Library项目中删除对Microsoft.AspNet.Core的引用,则一切正常.这是应从项目文件中删除的行:

If you remove the reference to Microsoft.AspNet.Core from the RclDemo.Library project, then everything works as expected. This is the line that should be removed from the project file:

<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />

如果创建新的.NET Core 3.x应用程序,则不会包含此引用,因为它现在已作为Microsoft.AspNetCore.App框架参考的一部分包含在内.静态文件在.NET Core 2.2 Razor类库中的工作方式不同,我认为您对Microsoft.AspNetCore.Mvc v2.2.0库的包含是令人毛骨悚然的事情.曾经有其他配置可以将静态文件包含在RCL中.

If you create a new .NET Core 3.x application, you would not include this reference as it is now included as part of the Microsoft.AspNetCore.App framework reference. Static files worked differently in .NET Core 2.2 Razor Class Libraries, and I think your inclusion of the Microsoft.AspNetCore.Mvc v2.2.0 library is breaking things. There used to be additional configuration to include static files in an RCL.

肖恩

这篇关于剃刀类库(RCL)中具有静态内容的404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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