Introduction To Mvc


Published on

Presentation at the SOCALCODECAMP

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Introduction To Mvc

  1. 1. Introduction to ASP.NET MVC Adnan Masood Volkan Uzun
  2. 2. About Adnan aka. Shameless Self Promotion <ul><li>Sr. Software Engineer / Tech Lead for Green Dot Corp. (Financial Institution) </li></ul><ul><li>Design and Develop Connected Systems. </li></ul><ul><li>Involved with SoCal Dev community, co-founded San Gabriel Valley .NET Developers Group. Published author and speaker. Winner of 2008-2009 Developer Community Champion Award. </li></ul><ul><li>MS. Computer Science, MCPD (Enterprise Developer), MCT, MCSD.NET </li></ul><ul><li>Doctoral Student - Areas of Interest: Machine learning, Bayesian Inference, Data Mining, Collaborative Filtering, Recommender Systems. </li></ul><ul><li>Contact at </li></ul><ul><li>Read my Blog at </li></ul><ul><li>REST and WCF 3.5 – Geek Speak Webcast </li></ul><ul><li>Doing a session in IASA 2008 in San Francisco on Aspect Oriented Programming; for details visit </li></ul>
  3. 3. About Me <ul><li>Web Developer at California State University, San Bernardino </li></ul><ul><li>Developing campus web sites, online courses, databases, securing the systems, making the sites accessible according 508 Guidelines </li></ul><ul><li>Active member of Inland Empire .NET user group </li></ul><ul><li>User group freak, attending average 5/6 users groups a month, driving 1000 miles average extra. </li></ul><ul><li>MS Computer Science, MCTS Sql Server 2005, MCP ASP.NET 2.0 </li></ul><ul><li>2007-2008 Most Valuable member of Inland Empire .NET user group </li></ul><ul><li>Not so active blog: </li></ul><ul><li>Trying to write better code by applying TDD (newbie!), reading other developers’ code (Scott Hanselman’s idea) </li></ul><ul><li>Lifetime learner </li></ul><ul><li>Email: </li></ul>
  4. 4. Agenda <ul><li>1. Introduction to MVC </li></ul><ul><li>2. Building your first MVC application (demo) </li></ul><ul><li>3. Understanding Controllers, Controller Actions, and Action Results </li></ul><ul><li>4. Understanding Models, Views, and Controllers </li></ul><ul><li>5. How to do Routing (demo) </li></ul><ul><li>6. How to do unit testing (demo) </li></ul><ul><li>7. How does YABE work (code walkthrough and demo) </li></ul><ul><li>8. Brief discussion of dependency injection et al </li></ul>
  5. 5. Top 5 MVC Resources <ul><li>ASP.NET MVC Home ( ) </li></ul><ul><li>Scott Guthrie’s Weblog ( </li></ul><ul><li>Scott Hanselman’s Blog ( ) </li></ul><ul><li>Phil Haack ( ) </li></ul><ul><li>ASP.NET Quick Start ( </li></ul>
  6. 6. ASP.NET MVC in a nutshell <ul><li>ASP.NET MVC enables you to build Model View Controller (MVC) applications by using the ASP.NET framework. ASP.NET MVC is an alternative, not a replacement, for ASP.NET Web Forms that offers the following benefits: </li></ul><ul><li>Clear separation of concerns </li></ul><ul><li>Testability - support for Test-Driven Development </li></ul><ul><li>Fine-grained control over HTML and JavaScript </li></ul><ul><li>Intuitive URLs </li></ul>
  7. 7. Highlights & Latest Developments in the MVC world <ul><li>MVC Beta has been released </li></ul><ul><li>Jquery will be coming with MVC Beta </li></ul><ul><li>More TDD support </li></ul><ul><li>Some functions are carried over between dll’s </li></ul>
  8. 8. MVC vs. “Classic” ASP.NET
  9. 9. Model View Controller Pattern
  10. 10. Why MVC? <ul><li>Very Clean separation of Concerns </li></ul><ul><li>Inherent Unit Testing Support </li></ul><ul><li>Model that leads you down a maintainable path (helps writing better code but doesn’t stop you writing crappy code) </li></ul><ul><li>Clean Urls and Html </li></ul>
  11. 11. Features <ul><li>Routing </li></ul><ul><li>Controller </li></ul><ul><li>View </li></ul><ul><li>Dependency Injection </li></ul><ul><li>Pluggable </li></ul><ul><li>Interfaces for all core contracts </li></ul>
  12. 12. DEMO <ul><li>Build your first ASP.NET MVC APP </li></ul>
  13. 13. MVC? <ul><li>A very brief explanation is: </li></ul><ul><li>Model: Your data source; database, text file, web services etc </li></ul><ul><li>View: Html page, what client sees </li></ul><ul><li>Controller: CPU of the mechanism, gets request from web server, communicates with Model to get data, returns a view to the requestor. A url request is a function inside controller class </li></ul>
  14. 14. Url <ul><li>Url no longer points to the view/aspx page </li></ul><ul><li>A Url maps to a controller class </li></ul><ul><li>Allows more flexibility to map Urls </li></ul><ul><li>Url routing engine, Route table </li></ul><ul><li>Routes are tokenized strings </li></ul><ul><li>Can use RegEx </li></ul>
  15. 15. Controllers & Actions <ul><li>Responds to requests </li></ul><ul><li>Interacts with Model, and renders Views </li></ul><ul><li>All controllers should have suffix “Controller” </li></ul><ul><li>Derives from System.Web.MVC.Controller class  </li></ul><ul><li>At the end, it is just a class, that has functions => Controller Actions that can’t be static, returns ActionResult </li></ul>
  16. 16. ActionResult & Actions <ul><li>Controller Actions return ActionResult </li></ul><ul><li>ActionResult can be: </li></ul><ul><li>ViewResult – Represents HTML and markup. </li></ul><ul><li>EmptyResult – Represents no result. </li></ul><ul><li>RedirectResult – Represents a redirection to a new URL. </li></ul><ul><li>RedirectToRouteResult – Represents a redirection to a new controller action. </li></ul><ul><li>JsonResult – Represents a JavaScript Object Notation result that can be used in an AJAX application. </li></ul><ul><li>ContentResult – Represents a text result. </li></ul><ul><li>Custom ActionResult </li></ul>
  17. 17. Model, View, Controller <ul><li>Controller: Gets the request from the requestor, (route mapping happens here), fires the action depending on the request </li></ul><ul><li>ActionMethod: communicates with Model to get the data returns ActionResult </li></ul><ul><li>ActionMethod can use ViewData, ViewData.Model or TempData to send data to View </li></ul><ul><li>View generates the markup language for the requestor. </li></ul><ul><li>View can use strongly typed Model </li></ul>
  18. 18. Views <ul><li>Similar to existing page </li></ul><ul><li>No postback or view state </li></ul><ul><li>Not all page events will fire </li></ul><ul><li>Editing forms will be more work </li></ul><ul><li>Separate page for viewing, editing, and updating </li></ul><ul><li>Ajax will use UserControls as update panels but </li></ul><ul><li>request still goes through the control </li></ul>
  19. 19. Views <ul><li>View probably shouldn’t directly access data sources or services. </li></ul><ul><li>You should put the business logic inside the views; try to stay away from <%if … </li></ul><ul><li>Use the ViewData which stores the model the view is rendering </li></ul><ul><li>ViewPage<T>, ViewPage </li></ul><ul><li>No postback </li></ul>
  20. 20. DEMO <ul><li>Add a new controller Test, with model interaction </li></ul>
  21. 21. How much backward Compatibility is there? <ul><li>The MVC framework supports using the existing ASP.NET .ASPX, .ASCX, and .Master markup files as &quot;view templates&quot; (meaning you can easily use existing ASP.NET features like nested master pages, <%= %> snippets, declarative server controls, templates, data-binding, localization, etc).  It does not, however, use the existing post-back model for interactions back to the server.  Instead, you'll route all end-user interactions to a Controller class instead - which helps ensure clean separation of concerns and testability (it also means no viewstate or page lifecycle with MVC based views). </li></ul><ul><li>The ASP.NET MVC framework fully supports existing ASP.NET features like forms/windows authentication, URL authorization, membership/roles, output and data caching, session/profile state management, health monitoring, configuration system, the provider architecture, etc. </li></ul>
  22. 22. URL Routing <ul><li>URL Routing is mapping the incoming requests to controller’s action methods. </li></ul><ul><li>There are handler’s settings in the web.config </li></ul><ul><li>Routing table exists in Global.asax.cs </li></ul><ul><li>Application_Start() registers the routing table </li></ul><ul><li>You can use regex to define route mappings </li></ul><ul><li>Route mapping works top to bottom, first match is called </li></ul><ul><li>Download Phil Hack’s RouteDebug tool </li></ul>
  23. 23. Route Examples <ul><li><Controller>/<Action>/<Param> </li></ul><ul><li>http://domain/ Home/CustomerDetail/45 </li></ul><ul><li>Html.Link for creating outbound urls </li></ul><ul><li>Html.Link(<Friendly Name>, <Action>, <Controller>) </li></ul>
  24. 24. DEMO <ul><li>Add a new controller action , set the route </li></ul>
  25. 25. Unit Testing <ul><li>Mock Objects to simulate responses. </li></ul><ul><li>Don’t have to go through a page or view </li></ul><ul><li>Red/Green Testing </li></ul><ul><li>Create instance of controller </li></ul><ul><li>Uses Mock Objects </li></ul><ul><li>You will need depedency injection down the road </li></ul>
  26. 26. Future of ASP.NET MVC <ul><li>MVC for the Enterprise </li></ul><ul><li>Classic Asp.Net for quicker implementations </li></ul>
  27. 27. References <ul><li>Download the Latest MVC Beta from Codeplex </li></ul><ul><li>Quick Start Guides </li></ul>
  28. 28. Questions? <ul><li>ASP.NET MVC is a new paradigm for .NET Developers; a new way to think about programming web application. </li></ul><ul><li>Any Questions / Comments … feel free to contact [email_address] </li></ul><ul><li>Visit </li></ul>