How to access RouteData from an ASP.Net 5 Tag Helper in MVC 6

拥有回忆 提交于 2019-11-27 18:28:14

问题


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.

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


回答1:


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

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!



来源:https://stackoverflow.com/questions/32535755/how-to-access-routedata-from-an-asp-net-5-tag-helper-in-mvc-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!