Blazor WASM 页面路由

Blazor WASM Page Routing

有没有办法通过变量来定义razor组件的页面路由?我希望以这种方式完成的原因之一是每当我需要在另一个页面上定义重定向 link 时,我可以只使用该变量而不是再次对其进行硬编码。

默认情况下,当我们想将 razor 组件定义为页面时,我们会这样做

@page "/product/{id}"

我希望它在某种意义上是集中的,它看起来像这样

public class PageRoute
{
    public const string ProductInfo = "/product/{id}";
}

然后我将在剃须刀组件上将其称为

@page PageRoute.ProductInfo

I want it to be centralized in a sense, that it would look something like this ....

不,不是开箱即用的 Blazor。 @page = "/" 被预编译到属性中:

[Microsoft.AspNetCore.Components.RouteAttribute("/")]
public partial class Index : Microsoft.AspNetCore.Components.ComponentBase {
....
}

这是在编译时固定的。如果你想要一个动态路由器,你需要编写你自己的路由器。在 Interent 中搜索“Blazor 动态路由”和“Blazor 自定义路由”以获取更多信息。