什么是 ASP.NET Core 静态 Web 资产? [英] What are ASP.NET Core Static Web Assets?

查看:20
本文介绍了什么是 ASP.NET Core 静态 Web 资产?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HostBuilder.ConfigureWebHostDefaults()(最终调用 ConfigureWebDefaults).我想更好地理解它,因为我找不到关于它的文档.

There is a lot of hidden magic that occurs in HostBuilder.ConfigureWebHostDefaults() (that eventually calls ConfigureWebDefaults). I would like to understand it better as there is no documentation that I can find about it.

这段代码似乎正在加载一些静态文件.什么是静态网络资产,我们为什么需要它们?这与将静态资产嵌入 Blazor 库有关吗?

This code seems to be loading some static files. What are static web assets and why do we need them? Is this something to do with embedding static assets into libraries for Blazor?

builder.ConfigureAppConfiguration((ctx, cb) =>
{
    if (ctx.HostingEnvironment.IsDevelopment())
    {
        StaticWebAssetsLoader.UseStaticWebAssets(ctx.HostingEnvironment, ctx.Configuration);
    }
});

推荐答案

静态 Web 资产是从 Razor 类库 (RCL):

Static Web Assets are static files made available from a Razor Class Library (RCL):

RCL 可能需要可被 RCL 的消费应用引用的伴随静态资产.ASP.NET Core 允许创建包含可供消费应用使用的静态资产的 RCL.

An RCL may require companion static assets that can be referenced by the consuming app of the RCL. ASP.NET Core allows creating RCLs that include static assets that are available to a consuming app.

UseStaticWebAssets 插入额外的 文件提供程序(的实例StaticWebAssetsFileProvider),使用清单文件({environment.ApplicationName}.StaticWebAssets.xml 如果未通过 IConfiguration 设置)来确定从路径到基本路径的映射列表.

UseStaticWebAssets inserts additional file providers (instances of StaticWebAssetsFileProvider), using a manifest file ({environment.ApplicationName}.StaticWebAssets.xml if not set via IConfiguration) to determine a list of mappings from path to base path.

例如,当使用 ASP.NET Core Identity UI RCL 时,您的应用的清单文件如下所示:

As an example, when using the ASP.NET Core Identity UI RCL, the manifest file for your app looks something like this:

<StaticWebAssets Version="1.0">
    <ContentRoot BasePath="/Identity" Path="path	o.nugetpackagesmicrosoft.aspnetcore.identity.ui3.0.0staticwebassetsV4" />
</StaticWebAssets>

这一切都以 CompositeFileProvider 正在为 IWebHostEnvironment.WebRootFileProvider.这个复合提​​供者做了两件事:

This all ends up with a CompositeFileProvider being set for the IWebHostEnvironment.WebRootFileProvider. This composite provider does two things:

  1. 像往常一样处理 wwwroot/ 静态文件(假设默认配置).
  2. 将从 wwwroot/Identity 请求的所有文件委托给已提取的用于 Identity UI 的 NuGet 包内容文件夹.
  1. Processes the wwwroot/ static files as usual (assuming default configuration).
  2. Delegates any files requested from wwwroot/Identity to the extracted NuGet package content folder for the Identity UI.

正如您问题中的代码片段所示,这只发生在 Development 环境中运行时.当您的应用发布时,相关文件会被复制到 wwwroot 文件夹中,就好像它们是您应用的一部分一样.

As the code snippet from your question shows, this only happens when running in the Development environment. When your app is published, the files in question get copied up into the wwwroot folder, as though they were part of your app.

这篇关于什么是 ASP.NET Core 静态 Web 资产?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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