introduction to asp.net mvc 2
DESCRIPTION
Denver Visual Studio User GroupTRANSCRIPT
Introduction to ASP.NET MVC 2
Joe WilsonVolare Systems, Inc.
Email: [email protected]: http://VolareSystems.com/Blog
Twitter: joe_in_denver
Agenda
What is MVC?
What are the benefits of MVC?
Code walkthroughs
Drawbacks of MVC
Future of MVC
Resources
Quick Audience Poll
Who has used Web Forms and not
MVC?
Who has used MVC?
Where does MVC fit?
ASP.NET
(Request, Response, Session, Cookies, QueryString, Master Pages)
Web Forms MVC
What is MVC?
M = Model
V = View
C = Controller
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
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.
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.)
What about a bigger MVC project?
http://whocanhelpme.codeplex.com
How MVC Works
Routing Engine
Controller (Action)
View,JSON, File,XML
Model
Request (GET or POST)
Response
MVC Conventions
http://server/product/edit
Action
Controller
Folder View
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
What are the benefits of MVC?
Style Sheets, Images
Controllers
ModelsJavaScript
Views
Master Pages
Tests
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
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)
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" />
What are the benefits of MVC?
Benefit: Clean URLs
MVC has more hackable, SEO-friendly URLs
URLs without query strings
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
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
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
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
Code!
Hello World MVC
Code!
Contact Form with Validation
Templates – EditorForModel()
Templates
Templates – Customize
Code!
Routing
Code!
Testing
Recap - Key Benefits of MVC
Simple web framework
Separation of Concerns
Convention over
configuration
Easier testing
Model Binding
Validation in the model
Templates
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
Drawbacks of MVC – jqGrid Screen
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.)
Future of MVC (MVC 3, Preview 1)
New Razor view engine
.CSHTML files
Uses @ to escape to C# instead of <% %>
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