简体   繁体   English

Blazor 服务器 - static 文件在非 DEV 环境中不链接

[英]Blazor Server - static files don't link in non-DEV environments

It seems in a standard Blazor server app, the _content folder items are not being referenced correctly for anything other than the Development environment.似乎在标准的 Blazor 服务器应用程序中,除了开发环境之外,没有正确引用 _content 文件夹项目。 As an example, this reference fails in any non-dev environment:例如,此引用在任何非开发环境中都失败:

from _Host.cshtml:来自 _Host.cshtml:

<link href="_content/Blazored.Typeahead/blazored-typeahead.css" rel="stylesheet" />

To Repro, using Blazored-toast lib as an example (but any static file refs seem to have this issue):以 Repro 为例,使用 Blazored-toast lib 作为示例(但任何 static 文件参考似乎都有这个问题):

Create a new Blazor Server project (dotnet new blazorserver)新建 Blazor 服务器项目(dotnet new blazorserver)

  1. Add all necessary Blazored / Toast elements, including code to demo a toast message添加所有必要的 Blazored / Toast 元素,包括演示 toast 消息的代码
  2. Test that toast is working Change launchSettings.json ASPNETCORE_ENVIRONMENT to Staging, Production, or anything other than Development测试 toast 是否正常工作 将 launchSettings.json ASPNETCORE_ENVIRONMENT 更改为暂存、生产或开发以外的任何内容
  3. Run program again (using ISS Express local debug), and notice the css formatting is not correct再次运行程序(使用 ISS Express 本地调试),注意到 css 格式不正确
  4. Change debug settings to use Kestrel instead (change IIS Express drop-down to BlazorApp1 or similar)更改调试设置以改用 Kestrel(将 IIS Express 下拉菜单更改为 BlazorApp1 或类似的)
  5. Notice with kestrel, css is working fine注意红隼,css 工作正常

What am I missing that would allow this reference to work in other environments?我错过了什么可以让这个引用在其他环境中工作?

Consuming static assets from a Razor Class Library works out of the box when the application gets published.当应用程序发布时,从 Razor Class 库中使用 static 资产即可使用。 You just have to include the static content via <link href="..." /> as you did.您只需像您一样通过<link href="..." />包含 static 内容。

However, when running the app from the build output (dotnet run) or via F5 in Visual Studio you have to ensure that the StaticWebAsset feature is enabled for the given environment.但是,当从构建 output(dotnet run)或通过 Visual Studio 中的 F5 运行应用程序时,您必须确保为给定环境启用了 StaticWebAsset 功能。

It is enabled by default for the development environment only.默认情况下,它仅对开发环境启用。 You can turn on the feature unconditionally by ensuring you called UseStaticFiles and calling UseStaticWebAssets in the Program.CreateHostBuilder .您可以通过确保在Program.CreateHostBuilder中调用UseStaticFiles并调用UseStaticWebAssets来无条件地打开该功能。

So, ensure that you consuming app has:因此,请确保您使用的应用程序具有:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    ...

    app.UseStaticFiles();

    ...
}

and in your Program.cs you should have在你的 Program.cs 中你应该有

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStaticWebAssets();
            webBuilder.UseStartup<Startup>();
        });

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

相关问题 无法在 NGINX 代理后面的 Blazor 服务器上加载 static 文件 - Can't load static files on Blazor server behind NGINX proxy Blazor(服务器端)身份验证和 static 文件 - Blazor (server side) authentication and static files 在已发布主机上引用 Blazor 服务器端中的静态文件 - Reference Static Files in Blazor Server Side When on Published Host 如何在 Z4681ZF7FA AppC81259CEF8E959C624DF1D456E5D3297Z 文件(ProjectName.style.css 和 blazor.server.js)中重命名捆绑的 static 文件(ProjectName.style.css 和 blazor.server.js) - How to rename bundled static files(ProjectName.style.css and blazor.server.js) in The Blazor App 为什么我在浏览器 > 开发工具 > 网络中看不到 DLL 和 do.net.wasm 被加载到 Blazor WebAssembly 应用程序上? - Why I don't see DLLs and dotnet.wasm being loaded on a Blazor WebAssembly app in Browser > Dev Tools > Network? 在 Cookie 中保存动态 CultureInfo 的 Blazor 服务器应用程序在 iFrame 中不起作用 - Blazor Server App with Dynamic CultureInfo Saved in Cookie don't work in iFrame 读取 ASP.NET Blazor 中的 static 文件 - Reading static files in ASP.NET Blazor Static class 与 Blazor 服务器中的注入 - Static class Vs injection in Blazor server 在 blazor 托管项目中提供静态文件 - Serve static files in blazor hosted project 使用 Blazor 读取服务器端文件 - Reading server-side files using Blazor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM