Introduction To ASP.Net MVC

23
Introduction to ASP.NET MVC Joe Wilson, President Volare Systems, Inc. Email: [email protected] Office: 303-532-5838, ext 101 Web: http://VolareSystems.com Blog: http://VolareSystems.com/Blog Twitter: joe_in_denver

Transcript of Introduction To ASP.Net MVC

Page 1: Introduction To ASP.Net MVC

Introduction to ASP.NET MVC

Joe Wilson, PresidentVolare Systems, Inc.

Email: [email protected]: 303-532-5838, ext 101

Web: http://VolareSystems.comBlog: http://VolareSystems.com/Blog

Twitter: joe_in_denver

Page 2: Introduction To ASP.Net MVC

Quick Audience Poll

Who has done classic ASP

development?

Who has used Web Forms?

Who has used MVC?

Page 3: Introduction To ASP.Net MVC

Agenda

What is MVC?

What problems is MVC trying to solve?

Code!

Drawbacks of MVC

Resources

Page 4: Introduction To ASP.Net MVC

What is MVC?

M = Model

V = View

C = Controller

Page 5: Introduction To ASP.Net MVC

What is MVC? – Views

Are familiar .ASPX and .ASCX files

Holds user interface elements (HTML, CSS,

JavaScript)

Views should not contain much logic

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

Page 6: Introduction To ASP.Net MVC

What is MVC? – Controllers

Are .CS files

Holds commands and workflows in Actions

(methods)

Render Views, JSON, XML, RSS, File, etc.

Works with Models for small logic and data

Validate user input before moving on

Page 7: Introduction To ASP.Net MVC

What is MVC? – Models

Are .CS files

Data to display in the View

Data posted from the View back to the Controller

Good place for validation attributes

Good place for light, display-related logic (Full

Name, Totals, IsUserLoggedIn, etc.)

Page 8: Introduction To ASP.Net MVC

What is MVC?

Routing Engine

Controller (Action)

View,JSON, File,

XML, RSS

Model

Request (GET or POST)

Response

Page 9: Introduction To ASP.Net MVC

What is MVC?

Controller View

View Model

Domain Model

Repositories

Utilities

Application Services

UILayer

Application Layer

Request Response

Page 10: Introduction To ASP.Net MVC

What else is MVC?

Part of ASP.NET

Not an upgrade or replacement for Web Forms

Can run side-by-side with Web Forms code

Cookies, Session, Caching, etc. still work the

same

Extensible for changing Routing, View Engine,

Model Binders, Action Filters, Controller Factories

Page 11: Introduction To ASP.Net MVC

What problem is MVC trying to solve?

Problem: No Separation of Concerns guidance

Don’t just dump all your code in code behind files

MVC gives guidance about what kind of code

goes where

Page 12: Introduction To ASP.Net MVC

What problem is MVC trying to solve?

Style Sheets, Images

Controllers

ModelsJavaScript

Views

Master Pages

Tests

Page 13: Introduction To ASP.Net MVC

What problem is MVC trying to solve?

Problem: View State and Page Bloat

View state is really helpful for rapid development

Not so nice for page size

Trade-off of rapid development with server

controls and large page size

MVC has no View State

Page 14: Introduction To ASP.Net MVC

What problem is MVC trying to solve?<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"

value="/wEPDwUKMTY1MzUzMjI0Ng9kFgwCAg8VAXonaHBfbmxhX3RyZWUnLCAnc2l0ZT11cycsICdwYXJ0bmVyPXVzJywgJ2xvZ2dlZGluPUZhbHNlJywgJ2luc3Q9RmFsc2UnLCAncHJvbW9FbGlnaWJsZT1UcnVlJywgJ2xhbmd1YWdlPUVuZ2xpc2gnLCAnaHA9bmxhJ2QCAw8VApQBJ2hwX25sYV9mcmVlJywgJ2hwVGVzdENlbGw9ZGVmYXVsdF9ubGEnLCAnc2l0ZT11cycsICdwYXJ0bmVyPXVzJywgJ2xvZ2dlZGluPUZhbHNlJywgJ2luc3Q9RmFsc2UnLCAncHJvbW9FbGlnaWJsZT1UcnVlJywgJ2xhbmd1YWdlPUVuZ2xpc2gnLCAnaHA9bmxhJ3wnaHBfbmxhX3NlYXJjaCcsICdzaXRlPXVzJywgJ3BhcnRuZXI9dXMnLCAnbG9nZ2VkaW49RmFsc2UnLCAnaW5zdD1GYWxzZScsICdwcm9tb0VsaWdpYmxlPVRydWUnLCAnbGFuZ3VhZ2U9RW5nbGlzaCcsICdocD1ubGEnZAIIDxUBfCdocF9ubGFfd2lkZ2V0JywgJ3NpdGU9dXMnLCAncGFydG5lcj11cycsICdsb2dnZWRpbj1GYWxzZScsICdpbnN0PUZhbHNlJywgJ3Byb21vRWxpZ2libGU9VHJ1ZScsICdsYW5nd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0cnkuY29tDlBSSVZBQ1kgUE9MSUNZEHd3dy5hbmNlc3RyeS5jb20UVGVybXMgYW5kIENvbmRpdGlvbnNkZI5o1QBkDUPswAnGKLEp0SlZ+Rv+" />

Guess which site?

Page 15: Introduction To ASP.Net MVC

What problem is MVC trying to solve?

Page 16: Introduction To ASP.Net MVC

What problem is MVC trying to solve?

Problem: Messy HTML

MVC controls keep their IDs

Easier to navigate with JavaScript and jQuery

Easier to build XHTML and Section 508 compliant

sites

Page 17: Introduction To ASP.Net MVC

What problem is MVC trying to solve?

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=“txtFromDate">From Date:</label><input type="text” name=“txtFromDate” id=“txtFromDate” />

Page 18: Introduction To ASP.Net MVC

What problem is MVC trying to solve?

Problem: Messy URLs

URLs without query strings

More hackable, SEO-friendly URLs

Page 19: Introduction To ASP.Net MVC

What problem is MVC trying to solve?

URLs for ASP.NET Web Formshttp://mysite.com/Shop.aspx?Category=Shirts

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

URLs for ASP.NET MVChttp://mysite.com/Shop/Category/Shirts

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

Page 20: Introduction To ASP.Net MVC

What problem is MVC trying to solve?

Problem: Web Forms code-behind testability

Code behind files are hard to test

HttpContext is hard to mock

MVC provides mockable abstract classes (but

mocking is still not as easy as it could be)

Page 21: Introduction To ASP.Net MVC

Code!

Page 22: Introduction To ASP.Net MVC

Drawbacks of MVC

Visual Studio web controls can’t help you much

No more OnClick server events for buttons, etc.

Different enough to have a learning curve

Ideas for better View engines still solidifying

Page 23: Introduction To ASP.Net MVC

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 Eric Hexter’s Blog - http://lostechies.com/blogs/hex Jimmy Bogard’s Blog - http://lostechies.com/blogs/jimmy_bogard

Email: [email protected]: 303-532-5838, ext 101

Web: http://VolareSystems.comBlog: http://VolareSystems.com/Blog

Twitter: joe_in_denver