[英]Can't add Route to Blazor Server Side
Ill try to register route via我会尝试通过以下方式注册路线
services.AddRazorPages(options => options.Conventions.AddPageRoute("/counter", "/c"));
in ConfigureServices
.在
ConfigureServices
中。
I can't just add @page "/c"
, because I plan to pull up this addresses from the config file.我不能只添加
@page "/c"
,因为我打算从配置文件中提取这些地址。
But, when I try to visit .../c
, it says that the page was not found, .../counter
works.但是,当我尝试访问
.../c
时,它说找不到该页面, .../counter
有效。 What could be the problem?可能是什么问题呢?
You may have a valid reason for resorting to AddPageRoute
, but you haven't said, so why don't you just do this:您可能有充分的理由诉诸
AddPageRoute
,但您还没有说,所以您为什么不这样做:
@page "/counter"
@page "/c"
@page "/co"
@page "/cou"
<h1>Counter</h1>
Unlike Razor, Blazor takes multiple @page
directives.与 Razor 不同,Blazor 采用多个
@page
指令。
RazorPages.= Blazor. RazorPages.= Blazor。
Your route does not apply to Blazor pages.您的路线不适用于 Blazor 页面。
When there was an .AddRazorPages()
in the original template code then that was from using Individual Accounts.当原始模板代码中有一个
.AddRazorPages()
时,那是来自使用个人帐户。 You can add a route for the Login, Logout and Account pages this way.您可以通过这种方式为登录、注销和帐户页面添加路由。 Not for /Counter.
不适用于 /Counter。
i want load address from config file and make redirect from this address to static page
我想从配置文件加载地址并从这个地址重定向到 static 页面
I made a demo for you:我为你做了一个演示:
@page "/"
@inject NavigationManager NavigationManager
@inject Microsoft.Extensions.Configuration.IConfiguration config
<button class="btn-primary" @onclick="RedirectTo">Go</button>
@code {
void RedirectTo()
{
var address = @config["address"];
NavigationManager.NavigateTo(address);
}
//Use this if you want to redirect immediately without some trigger event
protected override void OnInitialized()
{
var address = @config["address"]; //get address from config file
NavigationManager.NavigateTo(address); //redirect
}
}
Address configs in appsettings.json as an example:以 appsettings.json 中的地址配置为例:
{
"address": "/counter",
"Logging":...,
"AllowedHosts": "*"
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.