.net 将Blazor WASM移动到子目录(基本目录静态文件)
我想做一个应用程序,我需要在基本域上的静态页面和在子目录上的Blazor应用程序 例如:.net 将Blazor WASM移动到子目录(基本目录静态文件),.net,routes,blazor,static-files,blazor-webassembly,.net,Routes,Blazor,Static Files,Blazor Webassembly,我想做一个应用程序,我需要在基本域上的静态页面和在子目录上的Blazor应用程序 例如: https://example.com是我静态页面的位置(如“/features”或“/pricing”或“/faq”) https://example.com/app应该是我的blazor网站所在地 我看到我可以将路径字符串传递到app.useblazorframewfiles,但这不起作用(app.useblazorframewfiles(新路径字符串(“/app”))。 我的Blazor应用程序由gR
https://example.com
是我静态页面的位置(如“/features”或“/pricing”或“/faq”)
https://example.com/app
应该是我的blazor网站所在地
我看到我可以将路径字符串
传递到app.useblazorframewfiles
,但这不起作用(app.useblazorframewfiles(新路径字符串(“/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/" />
<StaticWebAssetBasePath>app</StaticWebAssetBasePath>
我已经看过Daz的答案,它确实解决了调试/运行独立产品时的问题。但是,我的生产环境基于Kubernetes,我的入口控制器不会将请求路由到正确的位置,例如: 在我的示例中,我更改了常见的4个设置:
- 将索引文件中的设置为
- 在servers ConfigureServices功能中设置app.UseBlazorFrameworkFiles(新路径字符串(“/app”))
- 在服务器配置函数的UseEndpoints选项中设置endpoints.MapFallbackToFile(“/app/{*path:nonfile}”,“app/index.html”)
- 将应用程序添加到csproj文件的客户端部分
// "/{app}/_content/{embedded-library-files}/{file}" requests need to be presented at root
app.Use(async (context, next) =>
{
HttpRequest request = context.Request;
var path = request.Path;
if (path.StartsWithSegments(new PathString("/app/_content")))
{
if (new string[] {
"/Microsoft.AspNetCore.Components.WebAssembly.Authentication/AuthenticationService.js"
}.Any(s => path.Value.EndsWith(s)))
{
request.Path = path.Value.Substring("/app".Length);
}
}
await next.Invoke();
});
每个文件夹都需要一个if子句,每个文件在列表中都需要一个条目。显然,如果您知道整个文件夹都应该重定向,这可以简化
由于重写服务器中的路径对于客户端应用程序是不可见的,所以这个简单的更新修复了这个问题
马克你把
放在你的Index.html中了吗?嗨@BrianParker,是的,我把'添加到了客户端/wwwroot/app/Index.html`中,它包含…
并加载blazor.webassembly.js
文件。第五位,添加app.UseStaticFiles(“/app”);
到startup.cs
。或者一些静态文件可能是404。如果您想从一台服务器托管多个Wasm应用程序,有一个演练,其中还列出了您需要调整路径的所有位置。
"Clients": {
"BlazorApp.Client": {
"Profile": "SPA",
"RedirectUri": "https://localhost:44357/app/authentication/login-callback",
"LogoutUri": "https://localhost:44357/app/authentication/logout-callback"
}
}
// "/{app}/_content/{embedded-library-files}/{file}" requests need to be presented at root
app.Use(async (context, next) =>
{
HttpRequest request = context.Request;
var path = request.Path;
if (path.StartsWithSegments(new PathString("/app/_content")))
{
if (new string[] {
"/Microsoft.AspNetCore.Components.WebAssembly.Authentication/AuthenticationService.js"
}.Any(s => path.Value.EndsWith(s)))
{
request.Path = path.Value.Substring("/app".Length);
}
}
await next.Invoke();
});