如何从 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 获取路线名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
how do i pass parameters to aspnet reportviewer(如何将参数传递给aspnet report查看器)
Bind multiple parameters from route and body to a model in ASP.NET Core(在ASP.NET Core中将路由和主体中的多个参数绑定到一个模型)
Custom model binding in AspNet Core WebApi?(AspNet Core WebApi中的自定义模型绑定?)
How to minify in .net core mvc view?(如何在.Net核心MVC视图中缩小?)