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

前端 未结 11 1766
旧巷少年郎
旧巷少年郎 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:06

    In the RC you can also extract route data like the action method name like this

    ViewContext.Controller.ValueProvider["action"].RawValue
    ViewContext.Controller.ValueProvider["controller"].RawValue
    ViewContext.Controller.ValueProvider["id"].RawValue
    

    Update for MVC 3

    ViewContext.Controller.ValueProvider.GetValue("action").RawValue
    ViewContext.Controller.ValueProvider.GetValue("controller").RawValue
    ViewContext.Controller.ValueProvider.GetValue("id").RawValue
    

    Update for MVC 4

    ViewContext.Controller.RouteData.Values["action"]
    ViewContext.Controller.RouteData.Values["controller"]
    ViewContext.Controller.RouteData.Values["id"]
    

    Update for MVC 4.5

    ViewContext.RouteData.Values["action"]
    ViewContext.RouteData.Values["controller"]
    ViewContext.RouteData.Values["id"]
    

提交回复
热议问题