[英]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)
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.