Introduction
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".
History
28 NOV 2017 - Initial post.