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

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

问题描述

在 ASP.NET Core (v3.1) 应用程序使用的 razor 类库 (.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 - Not Found.

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

我还与文档进行了交叉检查: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.

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 文件:wwwrootcssBase.css 我测试了以下路径:https://localhost:44300/_content/OurIt.Cockpit/css/Base.css,导致 404 Not found 响应.

I placed a css file in the library at the following location: wwwrootcssBase.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();.
  • 正确的大小写.
  • wwwrootcssBase.cssBuild 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 wwwrootcssBase.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

是否有机会调试或定位问题?参考微软文档:

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.

肖恩

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

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