Introduction to ASP.NET MVC
Agenda What's MVC What's ASP.NET MVC Goals of ASP.NET MVC How does it work?
What's MVC View Model Controller Defines application behavior Maps user action to model Select view to response Represent enterprise data Business rules that govern access/update data Render content of model
What's ASP.NET MVC New framework from Microsoft that builds on top standard ASP.NET engine ASP.NET Web Form ASP.NET MVC Core standard ASP . NET engine Microsoft .NET Framework
Goals of ASP.NET MVC Testability Pluggable Friendly URLs (REST URL) Existing ASP.NET feature still available Full control of HTML Open source
ASP.NET MVC ASP.NET MVC 1.0 (Dec, 2008) ASP.NET MVC 2.0 Beta (Nov, 2009)
ASP.NET MVC .NET Framework 3.5 Visual Studio 2008 (service pack 1) Download ( asp.net/mvc ) Visual Studio 2010 (included)
Demo: Hello World
Controller is responsible for handling incoming request Routing system decides how URLs map into particular controllers and actions How does it work? Action in controller Controller http://localhost:1725/ Hello / ActionTest
Component is first hit by a request The rules are configured in Global.asax Routing public static void RegisterRoutes(RouteCollection routes) { routes.MapRoute( "Default", "{ controller }/{ action }/{ id }", new { controller = "Home", action = "Index", id = "" } ); } http://localhost:1725/ Product / Show / 1
Demo: Model with Strong type
Request Flow Request View Controller Model HTTP Select View HTML Routing Select Controller
Demo: HTMLHelper
Provide method for creating HTML control Support Strong-type HTML Helper <%= Html. TextBoxFor (model => model. Body ) %> <input type=“text” name=“ Body ” id=“ Body ” />
Request variable Action method parameter ModelBinder Handling post request Public ActionResult Create() { string subject = Request.Form[“Subject”]; } Public ActionResult Create( string Subject ) { // do something } Public ActionResult Create( Commentary commentary ) { // do something }
Demo: Form & Validation
Validation Define Validation in Model Server Side Validation Client Side Validation ModelState Class to keep state of model Data Annotation Model Binder Java script
Summary RESTful URL Complex URL F ull control over   HTML Clean HTML Event - driven programming (Drag n’ drop) Design for testing Cannot test easily Separation of Concerns Combines code and view ASP.NET MVC ASP.NET WebForms
Q & A

Introduction to ASP.NET MVC

  • 1.
  • 2.
    Agenda What's MVC What's ASP.NET MVC Goals of ASP.NET MVC How does it work?
  • 3.
    What's MVC ViewModel Controller Defines application behavior Maps user action to model Select view to response Represent enterprise data Business rules that govern access/update data Render content of model
  • 4.
    What's ASP.NET MVCNew framework from Microsoft that builds on top standard ASP.NET engine ASP.NET Web Form ASP.NET MVC Core standard ASP . NET engine Microsoft .NET Framework
  • 5.
    Goals of ASP.NETMVC Testability Pluggable Friendly URLs (REST URL) Existing ASP.NET feature still available Full control of HTML Open source
  • 6.
    ASP.NET MVC ASP.NETMVC 1.0 (Dec, 2008) ASP.NET MVC 2.0 Beta (Nov, 2009)
  • 7.
    ASP.NET MVC .NETFramework 3.5 Visual Studio 2008 (service pack 1) Download ( asp.net/mvc ) Visual Studio 2010 (included)
  • 8.
  • 9.
    Controller is responsiblefor handling incoming request Routing system decides how URLs map into particular controllers and actions How does it work? Action in controller Controller http://localhost:1725/ Hello / ActionTest
  • 10.
    Component is firsthit by a request The rules are configured in Global.asax Routing public static void RegisterRoutes(RouteCollection routes) { routes.MapRoute( &quot;Default&quot;, &quot;{ controller }/{ action }/{ id }&quot;, new { controller = &quot;Home&quot;, action = &quot;Index&quot;, id = &quot;&quot; } ); } http://localhost:1725/ Product / Show / 1
  • 11.
    Demo: Model withStrong type
  • 12.
    Request Flow RequestView Controller Model HTTP Select View HTML Routing Select Controller
  • 13.
  • 14.
    Provide method forcreating HTML control Support Strong-type HTML Helper <%= Html. TextBoxFor (model => model. Body ) %> <input type=“text” name=“ Body ” id=“ Body ” />
  • 15.
    Request variable Actionmethod parameter ModelBinder Handling post request Public ActionResult Create() { string subject = Request.Form[“Subject”]; } Public ActionResult Create( string Subject ) { // do something } Public ActionResult Create( Commentary commentary ) { // do something }
  • 16.
    Demo: Form &Validation
  • 17.
    Validation Define Validationin Model Server Side Validation Client Side Validation ModelState Class to keep state of model Data Annotation Model Binder Java script
  • 18.
    Summary RESTful URLComplex URL F ull control over   HTML Clean HTML Event - driven programming (Drag n’ drop) Design for testing Cannot test easily Separation of Concerns Combines code and view ASP.NET MVC ASP.NET WebForms
  • 19.

Editor's Notes

  • #4 Main purpose of mvc is to isolate business logic from user interface pros - Better maintain - Testable - Cleaner code
  • #6 Testability - Framework provides interface-base  easy to mackable Pluggable - Every component can easy replaced or customize (view replaced with ironPython) - Easy to use dependency injection design pattern (unity, spring) Friendly URL - Can build app for search engine friendly Existing ASP,NET feature still available - All exist asp.net feature still available ex: Master.Page, User Control, Session, Memberdhip - But ViewState and page life cycle are not support Full control of HTML - Write only html - no viewstate Open source - under MS-PL license
  • #9 Demo from empty project Show create view and run Show ViewData and run Demo TDD hello world
  • #10 Controller is responsible for handling incoming request - All public method that implement by Controller class, It can be invoke from web via url.
  • #11 Controller is responsible for handling incoming request - All public method that implement by Controller class, It can be invoke from web via url.
  • #12 Demo new template project Modify routing default to commentary Create model Commentary Create Commentary controller Create View Show F5 Create Method Details Use Model.Subject
  • #14 Demo new template project Modify routing default to commentary Create model Commentary Create Commentary controller Create View Show F5 Show feature for HTML helper Show JSON JsonRequestBehavior.AllowGet
  • #17 Show validation with statemodel Show validation with anotation in serverside Show validation with client
  • #18 Using ModelState Class =&gt; Encapsulates the state of model binding to a property of an action - method argument, or to the argument itself
  • #19 Uses the ‘Page Controller’ pattern . Each page has a code - behind class that acts as a controller and is responsible for rendering the layout . Uses the ‘Front Controller’ pattern. There is a single central controller for all pages to process web application requests and facilitates a rich routing architecture