In the past I've stuck common properties, such as the current user, onto ViewData/ViewBag in a global fashion by having all Controllers inherit from a common base controller.
This allowed my to use IoC on the base controller and not just reach out into global shared for such data.
I'm wondering if there is an alternate way of inserting this kind of code into the MVC pipeline?
Best Answer
The best way is using the ActionFilterAttribute. I'll show you how to use it in .Net Core and .Net Framework.
.Net Core 2.1 & 3.1
Then you need to register this in your startup.cs.
.Net Core 3.1
.Net Core 2.1
Then you can use it in all views and pages
.Net Framework (ASP.NET MVC .Net Framework)
register your custom class in the global. asax (Application_Start)
Then you can use it in all views
Also there is another way
Creating an extension method on HtmlHelper
Then you can use it in all views