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

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

问题描述

HostBuilder.ConfigureWebHostDefaults()中发生了很多隐藏的魔术(最终会调用

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资产是可以从

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

RCL可能需要伴随的静态资产,该静态资产可以由RCL的使用应用程序引用. ASP.NET Core允许创建RCL,这些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 ),使用清单文件(如果未通过IConfiguration进行设置,则为{environment.ApplicationName}.StaticWebAssets.xml)来确定从路径到基本路径的映射列表.

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\to\.nuget\packages\microsoft.aspnetcore.identity.ui\3.0.0\staticwebassets\V4" />
</StaticWebAssets>

所有这些都以

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天全站免登陆