1 / 72

ASP.NET MVC in Action Austin Code Camp, 2009

ASP.NET MVC in Action Austin Code Camp, 2009. Jeffrey Palermo Chief Technology Officer Headspring Systems. Agenda. ASP.NET MVC History Getting Started The Model Controllers/Actions Views Routes AJAX MvcContrib Questions. ASP.NET MVC History.

sahara
Download Presentation

ASP.NET MVC in Action Austin Code Camp, 2009

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ASP.NET MVC in ActionAustin Code Camp, 2009 • Jeffrey Palermo • Chief Technology Officer • Headspring Systems

  2. Agenda ASP.NET MVC History Getting Started The Model Controllers/Actions Views Routes AJAX MvcContrib Questions

  3. ASP.NET MVC History • Oct 2007 – Scott Guthrie provides first glimpse at Alt.Net Conf in Austin, TX • Dec 2007 – First official CTP released • Dec 2007 – MvcContrib open source project launches • 2008 – 4 more CTPs released • Mar 2009 – First version released • Mar 2009 – MvcContrib 1.0 released • Today – Lots of buzz

  4. Getting Started - Pattern

  5. Getting Started - Components • .Net 3.5 SP1 • System.Web.Abstractions.dll • HttpContextBase • HttpRequestBase • etc • System.Web.Routing.dll • ASP.NET MVC v1.0 • System.Web.Mvc.dll • Extras • MvcContrib.dll • Microsoft.Web.Mvc.dll

  6. Getting Started - Responsibilities • Controllers • Responsible for WHAT a screen does. • Views • Responsible for DISPLAYING the screen. • Models • Responsible for REPRESENTING the task.

  7. Getting Started - Advantages • Decouples rendering logic from handling user input • Decouples screen logic from processing the http request • Leverages interfaces to separate responsibilities within the presentation layer • Provides complete control over markup rendered • Provides complete control over urls • Different presentation concerns can be independently tested

  8. Getting Started - Web Forms? ASP.NET • HttpApplication • HttpContext • HttpRequest • HttpResponse • HttpRuntime • HttpUtility • IHttpHandler • IHttpModule WebForms • Server Lifecycle • Postback • ViewState ASPX • MasterPages • Themes, Skins • General Templating

  9. Getting Started - ASP.NET MVC? ASP.NET • HttpApplication • HttpContext • HttpRequest • HttpResponse • HttpRuntime • HttpUtility • IHttpHandler • IHttpModule Mvc • Routes • Controllers • ViewData • Filters • MvcContrib ASPX • MasterPages • Themes, Skins • General Templating

  10. Getting Started – New Project

  11. Getting Started – Project Structure

  12. Getting Started - Routes • Old • http://codecampserver.com?group=adnug&meeting=april09 • ASP.NET MVC • http://codecampserver.com/adnug/april09 { key1}/{ key2 } • Default: {controller}/{action}/{id}http://foo.com/product/edit/22063748ProductController.cspublic ViewResult Edit(string id){…}

  13. Flow of an ASP.NET MVC Request • Request comes in to /Home • IIS determines the request should be handled by ASP.NET • ASP.NET gives all HttpModules a chance to modify the request • The UrlRoutingModule determines that the URL matches a route configured in the application

  14. Flow of an ASP.NET MVC Request • The UrlRoutingModule gets the appropriate IHttpHandler from the IRouteHandler that is used in the matching route (most of the time, MvcRouteHandler)as the handler for the request • The MvcRouteHandler constructs and returns MvcHandler. • The MvcHandler, which implements IHttpHandler executes ProcessRequest.

  15. Flow of an ASP.NET MVC Request • The MvcHandler uses IControllerFactory to obtain an instance of IController using the "controller" route data from the route {controller}/{action}/{id}. • The HomeController is found, and its Execute method is invoked. • The HomeController invokes the Index action.

  16. Flow of an ASP.NET MVC Request • The Index action adds some objects to the ViewData dictionary. • The HomeController invokes the ActionResult returned from the action, which renders a view. • The “index” view in the views folder displays the objects in ViewData. • The view, derived from System.Web.Mvc.ViewPage, executes its ProcessRequest method. • ASP.NET renders the response to the browser.

  17. The Model

  18. Model - Long-lived architecture • What causes legacy code? • Dependencies • Coupling • Lack of validated feedback (testing)

  19. Model - Layered Architecture UI Business Logic Data Access/Infrastructure

  20. Model - Layered Architecture UI Business Logic Data Access WCF I/O

  21. Model - Solution Structure Client Business Logic Web Service File Infrastructure Data Access DB

  22. Model - Onion Architecture SpeakerController IConferenceRepository IUserSession User Interface Domain Services UserSession <<class>> Domain Services Objects (aggregates) Web Service Application Core/Domain Model Tests File Infrastructure ConferenceRepository <<class>> DB

  23. Model - Solution Structure UI Core IoC Container Web Service File Infrastructure DB

  24. Model - Onion Architecture (flattened) UI I/O WCF Data Access Domain Services Aggregates (entities)

  25. Model - Data-Driven Architecture User Interface More Business Logic Business Logic Infrastructure DB Web Service File Tests Application Core

  26. Controllers

  27. Controllers – Icontroller

  28. Controllers – Action

  29. Controllers – Action Requirements • The method must be public. • The method cannot be a static method. • The method cannot be an extension method. • The method cannot be a constructor, getter, or setter. • The method cannot have open generic types. • The method is not a method of the controller base class. • The method cannot contain ref or out parameters.

  30. Controllers – Action return types

  31. Controllers – ActionResult

  32. Controllers – Parameter Binding

  33. Controllers – Parameter Binding public ActionResult Save(string conferenceKey, string firstName, string lastName, string email, string webpage) { //method body omitted. }

  34. Controllers – Parameter Binding • Precedence • Form values • Route arguments • Querystring parameters

  35. Controllers – Parameter Binding public class AttendeeForm { public virtual GuidConferenceID { get; set; } public virtual string FirstName { get; set; } public virtual string LastName { get; set; } public virtual string EmailAddress { get; set; } public virtual string Webpage { get; set; } } //resulting action method signature public ActionResult Save(AttendeeForm form){}

  36. Controllers – ModelBinding

  37. Controllers – Model Binding /conference/edit?conference=austincodecamppublic ActionResult Edit(Conference conference){…}

  38. Controllers – Registering Binders

  39. Controllers – ViewData

  40. Controllers – Filters

  41. Controllers – Action Filters • IActionFilter – before and after hooks when an action is executing • IResultFilter – before and after hooks when an action result is executing • IAuthorizationFilter – hooks when the controller is authorizing the current user • IExceptionFilter - hooks when an exception occurs during the execution of a controller

  42. Controllers – Provided ActionFilters • System.Web.Mvc.ActionFilterAttribute • System.Web.Mvc.OutputCacheAttribute • System.Web.Mvc.HandleErrorAttribute • System.Web.Mvc.AuthorizeAttribute • System.Web.Mvc.ValidateAntiForgeryTokenAttribute • System.Web.Mvc.ValidateInputAttribute

  43. Controllers – Applying Filters

  44. Controllers – Action Selectors • System.Web.Mvc.AcceptVerbsAttribute – limits action selection to requests of the specified verb type • System.Web.Mvc.NonActionAttribute – prevents action method from being selected

  45. Views

  46. Views – Folder Structure

  47. Views – IViewEngine

  48. Views – View not found

  49. Views – IView

  50. Views – WebFormViewEngine • Uses <%=…%> code nuggets • No code-behind • Leverages Master Pages • Partial views can be .aspx or .ascx • Responsibility is rendering markup

More Related