c# - 当我的应用程序使用不同的 appsettings 文件时,为什么 AspNetCore Identity 脚手架不会加载其关联的 CSS 和 JS 文件?

标签 c# angular asp.net-core asp.net-core-identity

我想添加一个“本地”appsettings 文件(称为“appsettings.Local.json”),其中包含用于在个人/本地计算机上调试我的 AspNetCore/Angular 应用程序(从默认 VS2019 模板创建)的信息。但是,当我将项目属性中的 ASPNETCORE_ENVIRONMENT 变量设置为“Local”时,当我尝试加载支架登录屏幕时,我在 Chrome 控制台中收到一堆有关 CSS 和 JS 文件未加载的错误。

enter image description here

enter image description here

无论如何,零意义是当我将 ASPNETCORE_ENVIRONMENT 变量从“本地”更改回“开发”时,这些错误消失并且登录屏幕正确呈现。

enter image description here

可能值得注意的是,我的 Startup.cs 文件看起来像这样(将 env.IsDevelopment() 切换为 env.IsEnvironment("Local");但无论我在这里使用什么,渲染都没有明显的变化登录屏幕)。

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsEnvironment("Local"))
        {
            app.UseDeveloperExceptionPage();
            app.UseDatabaseErrorPage();
        }
        else
        {
            app.UseExceptionHandler("/Error");
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }

        app.UseLoggingMiddleware(Configuration["TABLE_STORAGE_KEY"], Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"));

        app.UseHttpsRedirection();
        app.UseStaticFiles();
        if (!env.IsEnvironment("Local"))
        {
            app.UseSpaStaticFiles();
        }

        app.UseRouting();

        app.UseAuthentication();
        app.UseIdentityServer();
        app.UseAuthorization();
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller}/{action=Index}/{id?}");
            endpoints.MapRazorPages();
        });

        app.UseSpa(spa =>
        {
            // To learn more about options for serving an Angular SPA from ASP.NET Core,
            // see https://go.microsoft.com/fwlink/?linkid=864501

            spa.Options.SourcePath = "ClientApp";

            if (env.IsEnvironment("Local"))
            {
                //spa.UseAngularCliServer(npmScript: "start");
                spa.UseProxyToSpaDevelopmentServer("http://localhost:4200");
            }
        });
    }

可能相关:我最近也从 .NET Core 3.1 升级到 .NET 5,但我无法在网上找到任何关于这是导致我的问题的原因的信息。

编辑:5/12/2021 01: 当 ASPNETCORE_ENVIRONMENT 设置为“本地”时,相关 JS 和 CSS 文件似乎没有被复制到输出目录,因为我可以除非 ASPNETCORE_ENVIRONMENT 设置为“Development”,否则不会导航到 https://localhost:44395/Identity/lib/bootstrap/dist/js/bootstrap.bundle.min.js。但为什么会发生这种情况呢?

最佳答案

ASP.NET Core Identity UI 使用静态 Web Assets ,我在 this answer 中详细解释了这一点。 。本质上,这会将 /Identity/ 的 URL 映射到以如下所示的文件路径:

/path/to/.nuget/packages/microsoft.aspnetcore.identity.ui/*.*.*/staticwebassets/V4

如链接的问题和答案中所述,这只发生在开发环境中。在您的场景中,您需要将其用于 Local 环境,您可以在 Program.cs 中进行配置(假设典型设置),如下所示:

webBuilder.UseStartup<Startup>(); // This line is shown just for context.

// ...

webBuilder.ConfigureAppConfiguration((ctx, _) =>
{
    if (ctx.HostingEnvironment.IsEnvironment("Local"))
    {
        StaticWebAssetsLoader.UseStaticWebAssets(
            ctx.HostingEnvironment, ctx.Configuration);
    }
});

这可确保静态 Web Assets 映射也针对本地 环境运行。

关于c# - 当我的应用程序使用不同的 appsettings 文件时,为什么 AspNetCore Identity 脚手架不会加载其关联的 CSS 和 JS 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67498016/

相关文章:

C# 动态类型初始化器

c# - 从 ListView 中获取子项

angular - 在 @angular/google-maps map 中打开 infoWindow 时出现问题

c# - 如何在asp.net核心 Razor 页面MVVM之间传递或引用模型

c# - 如何计算/分析并行 C# 代码的百分比

c# - VS.Net 2008 中 Windows 服务的控制台输出

Angular5 - TypeScript 编译中缺少文件

Angular6如何测试一系列http请求

c# - 如何使用 FluentAssertions 在 XUnit 中测试 MediatR 处理程序

linux - 如何使用netcore在Linux centos 7中加载DevExpress报告