ASP.NET MVC 3

48
ASP.NET MVC 3 Buu Nguyen

description

Slides for my speech at Microsoft Technology Conference in Hanoi, Vietnam, March 2011

Transcript of ASP.NET MVC 3

Page 1: ASP.NET MVC 3

ASP.NET MVC 3 Buu Nguyen

Page 2: ASP.NET MVC 3

Buu Nguyen

• Microsoft MVP (ASP.NET)

• Vice President of Technology, KMS Technology

• Lecturer, RMIT University Vietnam

Page 3: ASP.NET MVC 3

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

Page 4: ASP.NET MVC 3

ASP.NET MVC OVERVIEW

Page 5: ASP.NET MVC 3

Technology Stack

Page 6: ASP.NET MVC 3
Page 7: ASP.NET MVC 3

Demo: Travel Log Web App

Page 8: ASP.NET MVC 3

NUGET

Page 9: ASP.NET MVC 3

NuGet - Package Manager for .NET

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

Page 10: ASP.NET MVC 3

Package Manager Console

Page 11: ASP.NET MVC 3

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

Page 12: ASP.NET MVC 3

Demo: Combres NuGet Package

Page 13: ASP.NET MVC 3

RAZOR VIEW ENGINE

Page 14: ASP.NET MVC 3

Web Forms vs. Razor

Page 15: ASP.NET MVC 3

Basic Syntax

Razor ASPX Description

@exp <%: exp %> Encode and output an expression to the page

@(exp) <%: exp %> Explicit expression, e.g. @(imagePath).jpg

@{ stms; } <% stms; %> Execute code statements

@Html.Raw(exp) <%= exp %> Output an expression to the page

@* cmt *@ <%-- cmt --%> Comment out code block

@if(cond) {

stm;

} else {

stms;

}

<%

if (cond) {

stms;

} else {

stms;

}

%>

Execute conditional statements Other constructs work the same way: @foreach, @for, @while, @switch, @try etc.

Page 16: ASP.NET MVC 3

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

Page 17: ASP.NET MVC 3

Transition to Markup

Option 3: Single line markup

Option 1: HTML Block

Option 2: Text Block

Page 18: ASP.NET MVC 3

Directives

or

Page 19: ASP.NET MVC 3

Functions

Page 20: ASP.NET MVC 3

Layout

Page 21: ASP.NET MVC 3

_ViewStart.cshtml

• Code in _ViewStart.cshtml executes before view rendering

• Hierarchical, subfolders override parent folders

Page 22: ASP.NET MVC 3

Razor Configuration

Page 23: ASP.NET MVC 3

Demo: Razor in Travel Log

Page 24: ASP.NET MVC 3

HTML HELPERS

Page 25: ASP.NET MVC 3

Declarative HTML Helpers

• Alternative way to write reusable rendering blocks

• Can be placed into App_Code folder

Page 26: ASP.NET MVC 3

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

Page 27: ASP.NET MVC 3

Demo: Declarative Helpers in Travel Log

Page 28: ASP.NET MVC 3

DEPENDENCY INJECTION

Page 29: ASP.NET MVC 3

Injecting Dependencies into Controllers

Page 30: ASP.NET MVC 3

• registering & injecting controller factories

• injecting controllers Controllers

• registering & injecting view engines

• injecting view pages Views

• locating & injecting filters Action Filters

• registering & injecting Model Binders

• registering & injecting Value Providers

• registering & injecting Validation Providers

• registering & injecting Model Metadata

Providers

DI Points in ASP.NET MVC 3

Page 31: ASP.NET MVC 3

IDependencyResolver

Page 32: ASP.NET MVC 3

Activators

• Useful when you don’t use an IoC container

Page 33: ASP.NET MVC 3

Demo: Dependency Injection in Travel Log

Page 34: ASP.NET MVC 3

MODEL VALIDATION

Page 35: ASP.NET MVC 3

Model Validation

• A value must be provided [Required]

• Value must be in a given range e.g. 1-10 [Range]

• Value must satisfy a regular expression [RegularExpression]

• Value must be a min length and less than the max length [StringLength]

• Value must equal another property e.g. password [Compare]

• Value is validated remotely via JSON [Remote]

• Custom model validators Extensible…

Page 36: ASP.NET MVC 3

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

Page 37: ASP.NET MVC 3

Demo: Remote Validator in Travel Log

Page 38: ASP.NET MVC 3

MINOR IMPROVEMENTS

Page 39: ASP.NET MVC 3

Sessionless Controllers

Page 40: ASP.NET MVC 3

ViewBag Property

Note: can be used exchangeably with ViewData dictionary

Page 41: ASP.NET MVC 3

JSON Model Binding

Page 42: ASP.NET MVC 3

Granular Input Validation

Page 43: ASP.NET MVC 3

Partial Page Output Cache

Page 44: ASP.NET MVC 3

Global Action Filters

Note: all MVC 3 filters are effectively singleton, don’t use instance states

Page 45: ASP.NET MVC 3

New Action Result Types

• HttpNotFoundResult

• HttpStatusCodeResult

• RedirectPermanent • RedirectToRoutePermanent

• RedirectToActionPermanent

Page 46: ASP.NET MVC 3

Demo: Apply to Travel Log

Page 47: ASP.NET MVC 3

Q&A

Page 48: ASP.NET MVC 3

THANK YOU! [email protected]

www.buunguyen.net/blog

www.twitter.com/buunguyen

http://vn.linkedin.com/in/buunguyen