How can I return the current action in an ASP.NET MVC view?

前端 未结 11 1765
旧巷少年郎
旧巷少年郎 2020-11-28 17:32

I wanted to set a CSS class in my master page, which depends on the current controller and action. I can get to the current controller via ViewContext.Controller.GetTy

11条回答
  •  粉色の甜心
    2020-11-28 18:11

    I vote for this 2:

    string currentActionName = ViewContext.RouteData.GetRequiredString("action");
    

    and

    string currentViewName = ((WebFormView)ViewContext.View).ViewPath;
    

    You can retrive both physical name of current view and action that triggered it. It can be usefull in partial *.acmx pages to determine host container.

提交回复
热议问题