Getting the name of the controller and action method in the view in ASP.Net MVC

后端 未结 2 1437
孤独总比滥情好
孤独总比滥情好 2020-12-01 09:24

Is there a way in a view in ASP.Net MVC to get the names of the controller and actien method that are using the view?

相关标签:
2条回答
  • 2020-12-01 09:37

    Try this:

    <%= ViewContext.RouteData.Values["Controller"] %>
    <%= ViewContext.RouteData.Values["Action"] %>
    
    0 讨论(0)
  • 2020-12-01 09:37
    <%= ViewContext.RouteData.Values["Controller"] %>
    <%= ViewContext.RouteData.Values["Action"] %>
    

    Be aware that if along the way, your routing has passed through a redirect of some kind, then the those values will still refer to the original requested controller/action, not the "current" one.

    So if a request to "/Home/Index" performs a redirect to "/Admin/Settings", then the above values will be "Home" and "Index" respectively and not "Admin" and "Settings" as perhaps may be expected or desired.

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