3

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.

2 Answers 2

2

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();
            }
        }
Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.