Introduction to ASP.NET MVC 2
Upcoming SlideShare
Loading in...5
×
 

Introduction to ASP.NET MVC 2

on

  • 3,912 views

Denver Visual Studio User Group

Denver Visual Studio User Group

Statistics

Views

Total Views
3,912
Views on SlideShare
3,909
Embed Views
3

Actions

Likes
2
Downloads
91
Comments
0

1 Embed 3

http://www.linkedin.com 3

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Introduction to ASP.NET MVC 2 Introduction to ASP.NET MVC 2 Presentation Transcript

  • Introduction to ASP.NET MVC 2
    Joe Wilson
    Volare Systems, Inc.
    Email: joe@volaresystems.com
    Blog: 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?
    Web Forms
    MVC
    ASP.NET
    (Request, Response, Session,
    Cookies, QueryString, Master Pages)
  • 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 Web Forms Works
    Event Based
    OnPreRender, OnLoad
    Page
    http://server/ProductEdit.aspx
    Request
    Response
    HTML, JSON, File, XML
  • How MVC Works
    Route Based
    Controller/Action
    Action
    Controller
    http://server/product/edit
    Request
    Response
    HTML, JSON, File, XML
  • How MVC Works
    Request (GET or POST)
    Routing Engine
    Controller (Action)
    Response
    View,
    JSON, File,
    XML
    Model
  • MVC Conventions
    Action
    Controller
    http://server/product/edit
    View
    Folder
  • 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
    Models
    JavaScript
    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
  • What are the benefits of MVC?
    Problem: No View State
    MVC has no View State
    View state is really helpful for rapid development
    Not so nice for page size
  • What are the benefits of MVC?
    <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTY1MzUzMjI0Ng9kFgwCAg8VAXonaHBfbmxhX3RyZWUnLCAnc2l0ZT11cycsICdwYXJ0bmVyPXVzJywgJ2xvZ2dlZGluPUZhbHNlJywgJ2luc3Q9RmFsc2UnLCAncHJvbW9FbGlnaWJsZT1UcnVlJywgJ2xhbmd1YWdlPUVuZ2xpc2gnLCAnaHA9bmxhJ2QCAw8VApQBJ2hwX25sYV9mcmVlJywgJ2hwVGVzdENlbGw9ZGVmYXVsdF9ubGEnLCAnc2l0ZT11cycsICdwYXJ0bmVyPXVzJywgJ2xvZ2dlZGluPUZhbHNlJywgJ2luc3Q9RmFsc2UnLCAncHJvbW9FbGlnaWJsZT1UcnVlJywgJ2xhbmd1YWdlPUVuZ2xpc2gnLCAnaHA9bmxhJ3wnaHBfbmxhX3NlYXJjaCcsICdzaXRlPXVzJywgJ3BhcnRuZXI9dXMnLCAnbG9nZ2VkaW49RmFsc2UnLCAnaW5zdD1GYWxzZScsICdwcm9tb0VsaWdpYmxlPVRydWUnLCAnbGFuZ3VhZ2U9RW5nbGlzaCcsICdocD1ubGEnZAIIDxUBfCdocF9ubGFfd2lkZ2V0JywgJ3NpdGU9dXMnLCAncGFydG5lcj11cycsICdsb2dnZWRpbj1GYWxzZScsICdpbnN0PUZhbHNlJywgJ3Byb21vRWxpZ2libGU9VHJ1ZScsICdsYW5ndWFnZT1FbmdsaXNoJywgJ2hwPW5sYSdkAgsPFQF4J2hwX25sYV9yMicsICdzaXRlPXVzJywgJ3BhcnRuZXI9dXMnLCAnbG9nZ2VkaW49RmFsc2UnLCAn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Code!Hello World MVC
  • Code!Contact Form with Validation
  • Templates – EditorForModel()
  • Templates
  • Templates – Customize
  • Templates – DisplayForModel()
  • Templates – DisplayForModel()
  • Templates – Customize
  • 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 <% %>
  • Future of MVC (MVC 3, Preview 1)
    Support for multiple view engines (Spark, Razor, Web Forms, NHaml, etc.)
    Common Service Locator for dependency resolution
    Global action filters (error handling, authorization)
  • Resources
    Learning 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: joe@volaresystems.com
    Blog: http://VolareSystems.com/Blog
    Twitter: joe_in_denver