问题
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