gpt4 book ai didi

.net - 将 Blazor WASM 移动到子目录(基本目录静态文件)

转载 作者:行者123 更新时间:2023-12-03 19:00:53 28 4
gpt4 key购买 nike

我想制作一个应用程序,我需要基域上的静态页面和子目录上的 Blazor 应用程序。
例子:https://example.com是我的静态页面应该在哪里(如“/features”或“/pricing”或“/faq”)https://example.com/app应该是我的 blazor 网站所在的地方。
我已经看到我可以传入 PathStringapp.UseBlazorFrameworkFiles但这不起作用( app.UseBlazorFrameworkFiles(new PathString("/app") )。
我的 Blazor 应用托管在带有 gRPC 的 .NET 5 WebApi 项目上。
有人可以指出我如何实现这一目标的正确方向吗?
提前致谢!

最佳答案

子目录名称需要出现在四个位置。
在您的 WebAPI 项目的 Startup.cs 中,您需要:

app.UseBlazorFrameworkFiles(new PathString("/app"));
并在端点配置中:
endpoints.MapFallbackToFile("/app/{*path:nonfile}", "app/index.html");
在 Wasm 应用程序的 wwwroot/index.html 文件中:
<base href="/app/" />
在 Wasm 应用程序的 CSProj 文件中:
<StaticWebAssetBasePath>app</StaticWebAssetBasePath>
文档: https://docs.microsoft.com/en-us/aspnet/core/blazor/host-and-deploy/webassembly?view=aspnetcore-5.0#hosted-deployment-with-multiple-blazor-webassembly-apps
这里有一个示例实现: https://github.com/javiercn/BlazorMultipleApps
如果您在 Identity 中使用了默认的 Wasm 模板,您会发现您的客户端无法正确加载,因为捆绑在 microsoft.aspnetcore.components.webassembly.authentication 中的静态 Assets 没有提供。要解决此问题,请将 index.html 中的 src 更改为绝对路径:
<script src="/_content/Microsoft.AspNetCore.Components.WebAssembly.Authentication/AuthenticationService.js"></script>
身份功能所需的其他更改:
  • 在 AppSettings 中,将 IdentityServer 客户端配置文件从“IdentityServerSPA”更改为“SPA”
  • 指定“RedirectUri”和“LogoutUri”以包含新的子目录
  •       "Clients": {
    "BlazorApp.Client": {
    "Profile": "SPA",
    "RedirectUri": "https://localhost:44357/app/authentication/login-callback",
    "LogoutUri": "https://localhost:44357/app/authentication/logout-callback"
    }
    }

    关于.net - 将 Blazor WASM 移动到子目录(基本目录静态文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64874081/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com