【发布时间】:2021-02-25 15:34:32
【问题描述】:
我已经设置了一个基本的 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>
【问题讨论】:
-
分享你的代码
-
真的没有什么特别要分享的。它是开箱即用的 .Net5 Blazor App WebAssembly 托管模型,已使用上述 URL 博客文章中所述的新预渲染进行设置。没有要显示的特定代码部分,它确实是整个项目
-
这取决于你如何设置它,所以有想法分享:_host.cshtml、Statup.cs 和 blazor app Program.cs
-
更新了服务器启动和_host文件
-
enet 并没有真正回答我的问题。我已将其设置为直接将其导航到失败的 URL 进行预渲染
标签: blazor webassembly .net-5