ASP.MVC3 @ TechDays2011
Introduction
Double goal of this new release
- simplify your code
- allow deeper extensibility.
The main improvement are
- The Razor View Engine
- Support for Multiple View Engines
- Controller Improvements
- JavaScript and Ajax
- Model Validation Improvements
- Dependency Injection Improvements
What’s new
The Razor View Engine
The syntax is concise.
IntelliSense support in Visual Studio.
-> short learning curv
Razor also includes new HTML helpers
Support for Multiple View Engines
Razor is the default View engine. but the user can use open source View engines
Controller Improvements
Global Action Filters
Sometimes you want to perform logic either before an action method runs or after an action method runs. To support this, ASP.NET MVC 2 provided action filters.
New “ViewBag” Property
MVC controllers support a ViewData property that enables you to pass data to a view template using a late-bound dictionary API. ViewBag is a dynamic property. It does the same that the ViewData
ViewData["Message"]="text", //is equivalent to ViewBag.Message="text".
New “ActionResult” Types
for example HttpNotFoundResult. Returns a 404 HTTP status code to the client.
HttpNotFound: This return type returns a error 404 on client.
RedirectResult:This returns a temporary redirect code 302 or permanent redirect code 302 depending upon a Boolean flag. This kind of redirection is very important for Search Engine optimization.
HttpStatusCodeResult: Returns a user specified code so developer have choice to return specific error code.
public ActionResult SpecificResult() { return new HttpStatusCodeResult(404); } public ActionResult NotFound() { return HttpNotFound(); } public ActionResult PermanentRedidrect() { return new RedirectResult("http://www.test.com"); }
JavaScript and Ajax
This is maybe one of the biggest simplification. Unobtrusive JavaScript avoids injecting inline JavaScript into HTML.
Client-Side Validation Enabled by Default
In order for client-side validation to work, you still need to reference the appropriate jQuery and jQuery Validation libraries in your site.
Remote Validator
A new RemoteAttribute class that enables you to use of the jQuery Validation plug-in’s remote validator support.
Generation des appels AJAX pour des validation cote client.
- in the model use the [Remote] attribute
- In the Controler the validation function will be implemented and returns JSONResult
- in the View a tag
Model Validation Improvements
public class User { [Required] public string Password { get; set; } [Required, Compare("Password")] public string ComparePassword { get; set; } }
Dependency Injection Improvements
..
NuGet Integration
ASP.NET MVC 3 automatically installs and enables NuGet as part of its setup. NuGet is a free open-source package manager that makes it easy to find, install, and use .NET libraries and tools in your projects.
Conclusion
The improvement from MVC 2 are aimed to simplify coding with the Razor view engine and integration with complex built in validation.