Blazor服务器-静态文件在非DEV环境中不链接 [英] Blazor Server - static files don't link in non-DEV environments

查看:92
本文介绍了Blazor服务器-静态文件在非DEV环境中不链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在标准的Blazor服务器应用程序中,似乎除开发环境以外的任何内容都未正确引用_content文件夹项.例如,此引用在任何非开发环境中均失败:

It seems in a standard Blazor server app, the _content folder items are not being referenced correctly for anything other than the Development environment. As an example, this reference fails in any non-dev environment:

来自_Host.cshtml:

from _Host.cshtml:

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

以Repro为例,以Blazored-toast lib为例(但任何静态文件引用似乎都存在此问题):

To Repro, using Blazored-toast lib as an example (but any static file refs seem to have this issue):

创建一个新的Blazor Server项目(dotnet新建blazorserver)

Create a new Blazor Server project (dotnet new blazorserver)

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

我缺少什么可以使该引用在其他环境中工作?

What am I missing that would allow this reference to work in other environments?

推荐答案

当应用程序发布时,使用Razor类库中的静态资产即开即用.您只需像以前一样通过<link href="..." />包含静态内容即可.

Consuming static assets from a Razor Class Library works out of the box when the application gets published. You just have to include the static content via <link href="..." /> as you did.

但是,从构建输出(dotnet运行)或通过Visual Studio中的F5运行应用程序时,必须确保为给定环境启用了StaticWebAsset功能.

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.

默认情况下仅在开发环境中启用它.您可以通过确保已调用UseStaticFiles并在Program.CreateHostBuilder中调用UseStaticWebAssets来无条件打开该功能.

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.

因此,请确保您使用的应用具有:

So, ensure that you consuming app has :

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

    app.UseStaticFiles();

    ...
}

在您的Program.cs中,您应该拥有

and in your Program.cs you should have

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

这篇关于Blazor服务器-静态文件在非DEV环境中不链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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