You are on page 1of 48

ASP.

NET MVC 3
Buu Nguyen

Buu Nguyen
Microsoft MVP (ASP.NET)

Vice President of Technology, KMS Technology


Lecturer, RMIT University Vietnam

ASP.NET MVC 3 Features


Major Improvements
NuGet Razor View Engine HTML Helpers Dependency Injection Model Validation

Minor Improvements
Sessionless Controllers ViewBag Property JSON Model Binding Granular Input Validation Partial Page Output Cache Global Action Filters

New Action Result Types

ASP.NET MVC OVERVIEW

Technology Stack

Demo: Travel Log Web App

NUGET

NuGet - Package Manager for .NET


Available at http://nuget.codeplex.com/

Package Manager Console

Building NuGet Package


Authoring support Define dependencies Transform configuration file and source Add initialization to web app startup via WebActivator

Contribute at http://nuget.org/Contribute/Index

Demo: Combres NuGet Package

RAZOR VIEW ENGINE

Web Forms vs. Razor

Basic Syntax
Razor
@exp @(exp) @{ stms; } @Html.Raw(exp) @* cmt *@ @if(cond) { stm; } else { stms; }

ASPX
<%: exp %> <%: exp %> <% stms; %> <%= exp %> <%-- cmt --%> <% if (cond) { stms; } else { stms; } %>

Description Encode and output an expression to the page Explicit expression, e.g. @(imagePath).jpg Execute code statements Output an expression to the page Comment out code block Execute conditional statements Other constructs work the same way: @foreach, @for, @while, @switch, @try etc.

Transition to Code
As soon as Razor parser encounters the syntax mentioned in

Basic Syntax, it switches to code mode Escape @ by using @@, e.g. Tweets by @@buunguyen

Transition to Markup
Option 1: HTML Block

Option 2: Text Block

Option 3: Single line markup

Directives

or

Functions

Layout

_ViewStart.cshtml
Code in _ViewStart.cshtml executes before view rendering

Hierarchical, subfolders override parent folders

Razor Configuration

Demo: Razor in Travel Log

HTML HELPERS

Declarative HTML Helpers


Alternative way to write reusable rendering blocks

Can be placed into App_Code folder

New HTML Helpers (both declarative & traditional)


Built-in
Chart WebGrid WebImage Crypto WebMail

microsoft-web-helpers
ReCaptcha LinkShare Gravatar

Bing
Analytics FileUpload Video Twitter FaceBook

Demo: Declarative Helpers in Travel Log

DEPENDENCY INJECTION

Injecting Dependencies into Controllers

DI Points in ASP.NET MVC 3


Controllers Views Action Filters Model Binders Value Providers
registering & injecting controller factories injecting controllers registering & injecting view engines injecting view pages locating & injecting filters

registering & injecting

registering & injecting

Validation Providers
Model Metadata Providers

registering & injecting

registering & injecting

IDependencyResolver

Activators
Useful when you dont use an IoC container

Demo: Dependency Injection in Travel Log

MODEL VALIDATION

Model Validation
[Required]
A value must be provided

[Range]

Value must be in a given range e.g. 1-10

[RegularExpression]

Value must satisfy a regular expression

[StringLength]

Value must be a min length and less than the max length

[Compare]

Value must equal another property e.g. password

[Remote]

Value is validated remotely via JSON

Extensible

Custom model validators

Other Validation Improvements


Self-validating model with IValidatableObject

ValidationContext for multi-property validation


Client-validation is enabled by default jQuery Validate plugin is used by default Unobtrusive client-side validation EF4 CTP5 works with validation attributes

Demo: Remote Validator in Travel Log

MINOR IMPROVEMENTS

Sessionless Controllers

ViewBag Property

Note: can be used exchangeably with ViewData dictionary

JSON Model Binding

Granular Input Validation

Partial Page Output Cache

Global Action Filters

Note: all MVC 3 filters are effectively singleton, dont use instance states

New Action Result Types


HttpNotFoundResult

HttpStatusCodeResult
RedirectPermanent
RedirectToRoutePermanent RedirectToActionPermanent

Demo: Apply to Travel Log

Q&A

THANK YOU!
buunguyen@kms-technology.com www.buunguyen.net/blog www.twitter.com/buunguyen http://vn.linkedin.com/in/buunguyen

You might also like