Click here to Skip to main content
14,272,574 members

What Controller Is This View For?

Rate this:
4.54 (10 votes)
Please Sign up or sign in to vote.
4.54 (10 votes)
28 Nov 2017CPOL
ViewBag, ViewBag, three times full...


While working on a MVC site, I decided I was tired of trying to remember which controller used the view in which I was working. Yes, my memory is that bad, especially considering I have over a dozen controllers in my project. And I'm old.

To make things easy on myself, I had already created a ControllerBase class from which all of my controllers already derive. I had done this so that I could create a string that represents a stylized html version of the site's name, and put it in the ViewBag. At that point, I could always get the stylized logo by referring to the ViewBag's variable instead of type/pasting all that dreary html (I love small, and seemingly pointless black-boxing around repetitive code). Since the infrastructure was already in place, adding the following line to the base class' constructor was an insignificant feat of software engineering prowess:

ViewBag.controllerName =  this.GetType().Name.Replace("Controller", "");

This allows me to do the following in all of my views, without worrying about what controller is controlling the view that's currently being controlled. (Is saying "controlled" considered to be a micro-aggression nowadays?)

@Html.ActionLink("ViewName", (string)ViewBag.controllerName)

Of course, there are times when you want to call views in other controllers, and you are still free to do just that. (This comment was added so that the more pedantic users won't feel the need to state the obvious.)

NOTE: No bacon was harmed in the creation of this tip/trick. Any resemblence to real-life bacon was not intentional and can generally be considered to be nothing more significant than "dumb luck".


28 NOV 2017 - Initial post.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior) Paddedwall Software
United States United States
I've been paid as a programmer since 1982 with experience in Pascal, and C++ (both self-taught), and began writing Windows programs in 1991 using Visual C++ and MFC. In the 2nd half of 2007, I started writing C# Windows Forms and ASP.Net applications, and have since done WPF, Silverlight, WCF, web services, and Windows services.

My weakest point is that my moments of clarity are too brief to hold a meaningful conversation that requires more than 30 seconds to complete. Thankfully, grunts of agreement are all that is required to conduct most discussions without committing to any particular belief system.

Comments and Discussions

PraiseNice Tip Pin
sestoenner1-Dec-17 4:47
membersestoenner1-Dec-17 4:47 
Nice tip, John... also like your bio... your history as a programmer is very much similar to mine.


General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 28 Nov 2017

Tagged as


2 bookmarked