How do I get Route name from RouteData?(如何从 RouteData 获取路线名称?)
问题描述
我在 Global.asax 中定义了几条路线;
I have several routes defined in my Global.asax;
当我在一个页面上时,我需要弄清楚当前路由的路由名称是什么,因为路由名称驱动着我的站点菜单.
When I'm on a page I need to figure out what is the route name of the current route, because route name drives my site menu.
如何做到这一点?
推荐答案
遗憾的是,无法获得路线的路线名称,因为该名称不是路线的属性.将路由添加到 RouteTable 时,名称将用作路由的内部索引,并且永远不会公开.
Unfortunately, it's not possible to get the route name of the route because the name is not a property of the Route. When adding routes to the RouteTable, the name is used as an internal index for the route and it's never exposed.
有一种方法可以做到这一点.
There's one way to do this.
当你注册一个路由时,在路由上设置一个带有路由名称的 DataToken 并使用它来过滤路由.
When you register a route, set a DataToken on the route with the route name and use that to filter routes.
实现#1 的最简单方法可能是编写自己的扩展方法来映射路由.
The easiest way to do #1 is to probably write your own extension methods for mapping routes.
这篇关于如何从 RouteData 获取路线名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何从 RouteData 获取路线名称?


- C#MongoDB使用Builders查找派生对象 2022-09-04
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 输入按键事件处理程序 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01