ASP.NET MVC 3 “Preview 1” includes several nice
controller-specific enhancements.
Global Filters
ASP.NET MVC supports the ability to declaratively apply
“cross-cutting” logic using a mechanism called “filters”. You can specify
filters on Controllers and Action Methods today using an attribute syntax like
so:
Developers often want to apply some filter logic across all
controllers within an application. ASP.NET MVC 3 now enables you to
specify that a filter should apply globally to all Controllers within an
application. You can now do this by adding it to the GlobalFilters
collection. A RegisterGlobalFilters() method is now included in the
default Global.asax class template to provide a convenient place to do this (it
is then called by the Application_Start() method):
The filter resolution logic in MVC 3 is flexible so that you
can configure a global filter that only applies conditionally if certain
conditions are met (for example: debugging is enabled, or if a request uses a particular
http verb, etc). Filters can also now be resolved from a Dependency
Injection (DI) container – more on that below.
New Dynamic ViewModel Property
ASP.NET MVC Controllers have supported a “ViewData” property
that enables you to pass data to a view template using a late-bound dictionary
API. For example:
The “ViewData” API is still supported in ASP.NET MVC
3. MVC 3 augments it, though, with a new “ViewModel” property on
Controller that is of type “dynamic” – and which enables you to use the new
dynamic language support within VB and C# to pass ViewData items using a slightly
cleaner syntax than the current dictionary API. Now you can alternatively
write the following code to achieve the same result as above:
You do not need to define any strongly-typed classes to use
the ViewModel property. Because it is a “dynamic” property you can
instead just get/set properties on it and it will resolve them dynamically at
runtime. It internally stores the property name/value pairs within the
ViewData dictionary.