Basically, I want to inject some data into ViewData/ViewBag for every single request.
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
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
RegisterGlobalFilters
method in yourGlobal.asax
:Now inside all your views you can use the
ViewBag.Foo
property.But in most situations Child Actions are a better alternative than
ViewBag
as they allow you to pass strongly typed view models instead of relying on this weakly typed ViewBag and some magic strings.