找不到 Blazor WASM .Net5 预渲染页面
Blazor WASM .Net5 Prerender Page Cannot Be Found
我已经设置了一个基本的 WASM 托管模板,并已转换为以下预渲染模板
https://jonhilton.net/blazor-wasm-prerendering/#commento-login-box-container
和
https://chrissainty.com/prerendering-a-client-side-blazor-application/
一切似乎都按预期工作,我可以单击导航链接并按预期更改页面。但是,我无法通过 URL 直接导航到页面。如果我输入 localhost:port/Counter 我会得到一个 Localhost 页面无法找到。当我单击计数器的导航链接时,它显示 URL 为 localhost:port/Counter.
为什么我无法直接导航到 URL?
已编辑:
这里有一些文件可以让您了解发生了什么。
这是服务器Startup.cs
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddRazorPages();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseWebAssemblyDebugging();
}
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.UseHttpsRedirection();
app.UseBlazorFrameworkFiles();
app.UseStaticFiles();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapControllers();
//endpoints.MapFallbackToFile("index.html");
endpoints.MapFallbackToFile("/_Host");
});
}
这里的改动是修改/_Host的mappfallbacktofile。这是添加到 Server/Pages 文件夹的新文件,如下所示
@page "/"
@using BlazorAppNet5WasmHosted.Client
@namespace BlazorAppNet5WasmHosted.Server.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title>BlazorAppNet5WasmHosted</title>
<base href="/" />
<link href="css/bootstrap/bootstrap.min.css" rel="stylesheet" />
<link href="css/app.css" rel="stylesheet" />
<link href="BlazorAppNet5WasmHosted.Client.styles.css" rel="stylesheet" />
</head>
<body>
<component type="typeof(App)" render-mode="WebAssemblyPrerendered" />
<script src="_framework/blazor.webassembly.js"></script>
</body>
</html>
在服务器启动中,我将 Fallback 更改为 /_Host
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapControllers();
//endpoints.MapFallbackToFile("index.html");
endpoints.MapFallbackToFile("/_Host");
});
然而问题是它不应该 endpoint.MapFallbackToFile("_Host");应该是 endpoints.MapFallbackToPage("/_Host");这就是导致刷新失败的原因
我已经设置了一个基本的 WASM 托管模板,并已转换为以下预渲染模板
https://jonhilton.net/blazor-wasm-prerendering/#commento-login-box-container
和
https://chrissainty.com/prerendering-a-client-side-blazor-application/
一切似乎都按预期工作,我可以单击导航链接并按预期更改页面。但是,我无法通过 URL 直接导航到页面。如果我输入 localhost:port/Counter 我会得到一个 Localhost 页面无法找到。当我单击计数器的导航链接时,它显示 URL 为 localhost:port/Counter.
为什么我无法直接导航到 URL?
已编辑:
这里有一些文件可以让您了解发生了什么。
这是服务器Startup.cs
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddRazorPages();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseWebAssemblyDebugging();
}
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.UseHttpsRedirection();
app.UseBlazorFrameworkFiles();
app.UseStaticFiles();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapControllers();
//endpoints.MapFallbackToFile("index.html");
endpoints.MapFallbackToFile("/_Host");
});
}
这里的改动是修改/_Host的mappfallbacktofile。这是添加到 Server/Pages 文件夹的新文件,如下所示
@page "/"
@using BlazorAppNet5WasmHosted.Client
@namespace BlazorAppNet5WasmHosted.Server.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title>BlazorAppNet5WasmHosted</title>
<base href="/" />
<link href="css/bootstrap/bootstrap.min.css" rel="stylesheet" />
<link href="css/app.css" rel="stylesheet" />
<link href="BlazorAppNet5WasmHosted.Client.styles.css" rel="stylesheet" />
</head>
<body>
<component type="typeof(App)" render-mode="WebAssemblyPrerendered" />
<script src="_framework/blazor.webassembly.js"></script>
</body>
</html>
在服务器启动中,我将 Fallback 更改为 /_Host
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapControllers();
//endpoints.MapFallbackToFile("index.html");
endpoints.MapFallbackToFile("/_Host");
});
然而问题是它不应该 endpoint.MapFallbackToFile("_Host");应该是 endpoints.MapFallbackToPage("/_Host");这就是导致刷新失败的原因