Introduction to ASP.NET MVC 2

34
Introduction to ASP.NET MVC 2 Joe Wilson Volare Systems, Inc. Email: [email protected] Blog: http://VolareSystems.com/Blog Twitter: joe_in_denver

description

Denver Visual Studio User Group

Transcript of Introduction to ASP.NET MVC 2

Page 1: Introduction to ASP.NET MVC 2

Introduction to ASP.NET MVC 2

Joe WilsonVolare Systems, Inc.

Email: [email protected]: http://VolareSystems.com/Blog

Twitter: joe_in_denver

Page 2: Introduction to ASP.NET MVC 2

Agenda

What is MVC?

What are the benefits of MVC?

Code walkthroughs

Drawbacks of MVC

Future of MVC

Resources

Page 3: Introduction to ASP.NET MVC 2

Quick Audience Poll

Who has used Web Forms and not

MVC?

Who has used MVC?

Page 4: Introduction to ASP.NET MVC 2

Where does MVC fit?

ASP.NET

(Request, Response, Session, Cookies, QueryString, Master Pages)

Web Forms MVC

Page 5: Introduction to ASP.NET MVC 2

What is MVC?

M = Model

V = View

C = Controller

Page 6: Introduction to ASP.NET MVC 2

What is MVC? – Views

Are .ASPX and .ASCX files

Inherit from ViewPage

Holds user interface elements (HTML, CSS,

JavaScript)

Views should not contain much logic

Simple “if”s or “foreach”es are OK

Page 7: Introduction to ASP.NET MVC 2

What is MVC? – Controllers

Are .CS files

Inherit from Controller

Code is in Actions (methods)

Validate user input before moving on

Works with Models for small logic and data

access

Render Views, JSON, XML, File, etc.

Page 8: Introduction to ASP.NET MVC 2

What is MVC? – Models

Are .CS files and just POCOs

MVC doesn’t dictate how to set up Models

Data to display in the View or post from the View

back to the Controller

Good place for validation attributes

Good place for light, display-related logic

(FullName, Totals, IsUserLoggedIn, etc.)

Page 9: Introduction to ASP.NET MVC 2

What about a bigger MVC project?

http://whocanhelpme.codeplex.com

Page 10: Introduction to ASP.NET MVC 2

How MVC Works

Routing Engine

Controller (Action)

View,JSON, File,XML

Model

Request (GET or POST)

Response

Page 11: Introduction to ASP.NET MVC 2

MVC Conventions

http://server/product/edit

Action

Controller

Folder View

Page 12: Introduction to ASP.NET MVC 2

What are the benefits of MVC?

Benefit: Separation of Concerns guidance

MVC gives guidance about what kind of code

goes where

Convention over configuration

Don’t dump all your code in Page_Load and

Button_Click

Page 13: Introduction to ASP.NET MVC 2

What are the benefits of MVC?

Style Sheets, Images

Controllers

ModelsJavaScript

Views

Master Pages

Tests

Page 14: Introduction to ASP.NET MVC 2

What are the benefits of MVC?

Benefit: Testability

MVC allows creation of a new Controller

inside a test

Web Forms code behind files are hard to test

because HttpContext is hard to mock

Page 15: Introduction to ASP.NET MVC 2

What are the benefits of MVC?

Benefit: Clean HTML

MVC controls keep their IDs

Easier to navigate with JavaScript and jQuery

Easier to build XHTML and Section 508 compliant

sites (use W3C Validators and Section508.info to

check)

Page 16: Introduction to ASP.NET MVC 2

What are the benefits of MVC?

HTML from Web Forms<span id="ctl00_MainContent_uxdPHAllContent_TripSelector_lblFromDate">From Date:</span><input type="text"

name="ctl00$MainContent$uxdPHAllContent$TripSelector$txtFromDate" id="ctl00_MainContent_uxdPHAllContent_TripSelector_txtFromDate" />

HTML from MVC<label for="FromDate">From Date:</label><input type="text" name="FromDate" id="FromDate" />

Page 17: Introduction to ASP.NET MVC 2

What are the benefits of MVC?

Benefit: Clean URLs

MVC has more hackable, SEO-friendly URLs

URLs without query strings

Page 18: Introduction to ASP.NET MVC 2

What are the benefits of MVC?

URLs for ASP.NET Web Forms

http://mysite.com/Shop.aspx?Category=Shirts

http://mysite.com/Shop.aspx?ID=123

URLs for ASP.NET MVC

http://mysite.com/Shop/Category/Shirts

http://mysite.com/Shop/ID/123

Page 19: Introduction to ASP.NET MVC 2

What are the benefits of MVC?

Benefit: Headroom

Extensible for changing

◦ Routing

◦ View Engine

◦ Model Binders

All code is released as open source

◦ Action Filters

◦ Controller Factories

Page 20: Introduction to ASP.NET MVC 2

What are the benefits of MVC?

Benefit: Simplicity

No more OnInit, OnPreRender, OnLoad events

No more OnItemDataBound events

No more

this.Parent.Parent.Parent.FindControl

Page 21: Introduction to ASP.NET MVC 2

What are the benefits of MVC?

Benefit: Can run alongside Web

Forms

Don’t have to wait for your next project

Just make changes to Web.configs

Page 22: Introduction to ASP.NET MVC 2

Code!

Hello World MVC

Page 23: Introduction to ASP.NET MVC 2

Code!

Contact Form with Validation

Page 24: Introduction to ASP.NET MVC 2

Templates – EditorForModel()

Page 25: Introduction to ASP.NET MVC 2

Templates

Page 26: Introduction to ASP.NET MVC 2

Templates – Customize

Page 27: Introduction to ASP.NET MVC 2

Code!

Routing

Page 28: Introduction to ASP.NET MVC 2

Code!

Testing

Page 29: Introduction to ASP.NET MVC 2

Recap - Key Benefits of MVC

Simple web framework

Separation of Concerns

Convention over

configuration

Easier testing

Model Binding

Validation in the model

Templates

Page 30: Introduction to ASP.NET MVC 2

Drawbacks of MVC

Visual Studio web controls can’t help you much,

but…

GridView => jqGrid or other jQuery plugins

Chart => Google Charts or a jQuery plugin

Menu => tons of jQuery plugins

Page 31: Introduction to ASP.NET MVC 2

Drawbacks of MVC – jqGrid Screen

Page 32: Introduction to ASP.NET MVC 2

Drawbacks of MVC

No more OnClick server events for buttons, etc.

Different enough to have a learning curve

Ideas for better View engines still solidifying (Web

Forms, Spark, NHaml, Razor, etc.)

Page 33: Introduction to ASP.NET MVC 2

Future of MVC (MVC 3, Preview 1)

New Razor view engine

.CSHTML files

Uses @ to escape to C# instead of <% %>

Page 34: Introduction to ASP.NET MVC 2

ResourcesLearning ASP.NET MVC ASP.NET MVC official site - http://www.asp.net/mvc Community Four MVC - http://www.c4mvc.net

Keeping up with changes to ASP.NET MVC Phil Haack’s Blog - http://haacked.com Scott Guthrie’s Blog - http://weblogs.asp.net/scottgu

Advanced ASP.NET MVC Sharp Architecture - http://sharparchitecture.net Who Can Help Me? - http://whocanhelpme.codeplex.com Eric Hexter’s Blog - http://lostechies.com/blogs/hex Jimmy Bogard’s Blog - http://lostechies.com/blogs/jimmy_bogard

Email: [email protected]: http://VolareSystems.com/Blog

Twitter: joe_in_denver