如何从 RouteData 获取路线名称?

How do I get Route name from RouteData?(如何从 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 获取路线名称?

基础教程推荐