The Transition to Razor
In January 2011, Microsoft released ASP.NET MVC 3. The most significant feature is the introduction of the Razor view engine. Previously, ASP.NET MVC relied on the ASPX view engine, which inherited bloated Web Forms markup (like <%= Html.Encode(Model.Name) %>).
Razor provides a clean, lightweight syntax optimized for HTML generation:
- ◆Clean markup: Prefixing C# commands with a single
@character. - ◆Auto-escaping: Automatic HTML encoding on output to mitigate Cross-Site Scripting (XSS) attacks.
- ◆Expressive code block layouts: Seamlessly transitioning between C# structures and standard HTML tags.
@* Typical CSHTML view file syntax in early 2011 *@
@model ShivamItcs.Models.DashboardViewModel
<div class="dashboard-card">
<h3>Welcome, @Model.Username</h3>
@if (Model.IsAdmin) {
<span class="badge">System Admin</span>
}
</div>Key Enhancements in MVC 3
ASP.NET MVC 3 introduces several runtime changes:
1. Dynamic ViewBag
C# 4's dynamic type support enables the ViewBag collection, replacing the string-indexed ViewData dictionary. It allows developers to transfer dynamic data from controller to view without compile-time check configurations.
2. Global Action Filters
Instead of decorating every single controller class, developers can now register filters globally (e.g. for security audits or request logging) in the global application setup.
3. Native Dependency Resolution
MVC 3 integrates a service locator pattern, simplifying integration with IoC (Inversion of Control) containers like StructureMap, Ninject, or Unity.