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

前端 未结 1 1227
夕颜
夕颜 2020-12-17 01:12

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 acc

1条回答
  •  囚心锁ツ
    2020-12-17 01:41

    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!

    0 讨论(0)
提交回复
热议问题