如何从 MVC 6 中的 ASP.Net 5 Tag Helper 访问 RouteData [英] How to access RouteData from an ASP.Net 5 Tag Helper in MVC 6

查看:10
本文介绍了如何从 MVC 6 中的 ASP.Net 5 Tag Helper 访问 RouteData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取当前路线,以便我可以使用标记帮助程序在一组链接中突出显示活动页面.

I am trying to get hold of the current route so that I can highlight the active page in a set of links using a Tag Helper.

TagHelperContext 不能让我访问任何有用的东西.如何获得对 RouteData 的引用?

The TagHelperContext doesn't give me access to anything useful. How can I get a reference to the RouteData?

推荐答案

我最终找到了这里描述的答案:https:///github.com/aspnet/Announcements/issues/28

I eventually found the answer described here: https://github.com/aspnet/Announcements/issues/28

您可以使用新属性通过属性注入来导入 ViewContext.您需要在标签助手类中创建一个属性,如下所示:

You can import the ViewContext using property injection by using a new attribute. You need to create a property in you tag helper class like this:

[ViewContext]
public ViewContext ViewContext { get; set; }

然后您可以像这样访问当前控制器或操作:

You can then access the current controller or action like so:

var pageController = ViewContext.RouteData.Values["controller"];
var pageAction = ViewContext.RouteData.Values["action"];

也许我在做足够的研究之前发布了这个问题,但这并不完全明显,所以我希望这对其他人有帮助!

Maybe I posted this question before doing enough research, but this was not entirely obvious, so I hope this helps someone else!

这篇关于如何从 MVC 6 中的 ASP.Net 5 Tag Helper 访问 RouteData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆