El *_*ppo 2 c# routing asp.net-core blazor-server-side
我开始使用Blazor服务器端,但我没有正确路由。我不明白的,需要base在标签_host.cshtml。如果我仍然必须base在每个 Blazor 组件中添加url,例如:我想要一个基地址,/app/并且@page示例的指令Counter值为 ,"/counter"不会“编译”到"/app/counter". 我必须设置@page到"/app/counter"这是有道理的,但是这意味着,base在标签_host.cshtml是没有用的......
我在这里出了什么问题?
这<base href='' >是一种客户端技术,用于为当前文档中的所有相关 URL 指定基本 URL。许多 SPA 框架,例如 Angular,都会使用这个元素。
我必须将@page 设置为“/app/counter”
其实,你不必和不应该做的。使用时最重要的优势之一<base>是它允许我们创建应用程序而无需让组件知道基本 url。
假设您已将基址href从“/”更改为“/app/”,并且还更改了其他相关网址:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>BlazorApp</title>
<base href="/app/" />
<link rel="stylesheet" href="/css/bootstrap/bootstrap.min.css" />
<link href="/css/site.css" rel="stylesheet" />
</head>
<body>
<app>
@(await Html.RenderComponentAsync<App>(RenderMode.ServerPrerendered))
</app>
<script src="/_framework/blazor.server.js"></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
不要忘记在默认Blazor集线器 url前面加上一个/app/:
app.UseEndpoints(endpoints =>
{
endpoints.MapBlazorHub("/app/_blazor");
endpoints.MapFallbackToPage("/_Host");
});
就这样。有没有必要从改变路线@page "/counter",以@page "/app/counter"在所有。
这是一个我们不更改@page组件路由的演示:
| 归档时间: |
|
| 查看次数: |
4458 次 |
| 最近记录: |