Basically, I want to inject some data into ViewData/ViewBag for every single request.
Basically, I want to inject some data into ViewData/ViewBag for every single request.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In ASP.NET MVC that would be an action filter. And if you want to do it globally you could register it as a global action filter. This way it will apply to all controller actions so that you don’t need to decorate them individually.
So your filter could be defined like this:
and registered in the
RegisterGlobalFiltersmethod in yourGlobal.asax:Now inside all your views you can use the
ViewBag.Fooproperty.But in most situations Child Actions are a better alternative than
ViewBagas they allow you to pass strongly typed view models instead of relying on this weakly typed ViewBag and some magic strings.