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?
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With