Get ControllerName and ActionName and populate the ViewData in Master Page?

Abdulsattar Mohammed picture Abdulsattar Mohammed · Jul 31, 2009 · Viewed 29.6k times · Source

I've a SuperController which will be inherited from all the controllers. In the constructor I'm trying to populate the ViewData using the ControllerName and the ActionName.

I'm not going to pass the ControllerName and the ActionName as the ViewData. I've a method that needs the ControllerName and the ActionName and I need to pass the return value of the method as the ViewData.

How can I do that?

Answer

eu-ge-ne picture eu-ge-ne · Jul 31, 2009

Don't use ViewData. Use this in your Site.Master instead:

<%= ViewContext.RouteData.Values["Controller"] %>
<%= ViewContext.RouteData.Values["Action"] %>

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