ilija veselica
ilija veselica

Reputation: 9574

asp.net mvc - Check what controller and method are called?

How to check what controller and method are called?

In html in body tag there is: <body id="somethingThatDependsOnControllerAndMethod">. id value will be assigned based on controller and it's method.

Upvotes: 3

Views: 3957

Answers (2)

Dale Ragan
Dale Ragan

Reputation: 18270

Check out my answer to this question. It describes how to get the info you need within your controller and then you can pass that info down to your view using a view model or the ViewData property bag.

I prefer this method, because it gives my view everything it needs to do it's job. I also don't like using the ViewContext, because it means I have to use strings to access it's dictionary for what I need and I prefer to keep everything strongly typed.

Upvotes: 2

Mark Redman
Mark Redman

Reputation: 24535

I have this in my MasterPage to get this info to display certain things on-screen, might help with the info you need (might not be the most elegant solution)

 public string ControllerActionName
        {
            get
            {
                return String.Concat(ViewContext.RouteData.Values["Controller"], ViewContext.RouteData.Values["Action"]).ToLower();
            }
        }

Upvotes: 2

Related Questions