给定在 Blazor 服务器端使用的 Razor 页面/组件
@page "/items/create" // create route
@page "/items/edit/{id:long}" // edit route
在我的代码中,我需要知道 create route
或 edit route
被使用。我该如何检查?我已经知道
NavigationManager.Uri
但这并不是真正的错误证明,例如NavigationManager.Uri.Contains("edit")
http://localhost:123/items/create/?edit=faked_edit
也将评估为真(所以会声称使用了 edit route
,尽管使用了 create route
)。
最佳答案
在我的情况下,我不得不做类似的事情,但找不到合适的解决方案,但检查整个路径的解决方法对我有用。您可以使用以下示例:NavigationManager.Uri.Contains("/items/edit/")
此外,在一个地方,我还检查了 id
不为 null 或为空,这将是编辑页面。
关于routes - 如何检查用于调用页面/组件的路由?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63678213/