gpt4 book ai didi

找不到 Blazor WASM .Net5 预渲染页面

转载 作者:行者123 更新时间:2023-12-02 02:32:32 25 4
gpt4 key购买 nike

我已经设置了一个基本的 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?

已编辑:

这里有一些文件可以让您了解发生了什么。

这是Server 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");这就是导致刷新失败的原因

关于找不到 Blazor WASM .Net5 预渲染页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64824091/

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