150 likes | 456 Views
ASP.NET MVC. John Clayton http://codemonkeylabs.com/. What is ASP.NET MVC?. MVC == Model, View, Controller Model: The concrete ‘things’ you are working on View: The output rendered to the client Controller: Where the logic lives Gives you complete control over URLs and markup
E N D
ASP.NET MVC John Clayton http://codemonkeylabs.com/
What is ASP.NET MVC? • MVC == Model, View, Controller • Model: The concrete ‘things’ you are working on • View: The output rendered to the client • Controller: Where the logic lives • Gives you complete control over URLs and markup • Build AJAX applications with speed and ease • Easily extensible at any point of the request • Make it do what you want • Designed with automated testing in mind • Because we’re all human
What about Web Forms? • Web Forms isn’t going away • Improvements in ASP.NET 4.0 will make it easier for Web Forms and MVC to coexist • Web Forms & MVC are both rendering engines on top of the ASP.NET runtime • MVC uses the Web Forms engine by default
How it works • When a request comes in it is matched against a route • The route provides a route handler • The route handler provides a controller • An action in the controller does the work and provides an action result • The action result is executed to return content to the client • Any part of this can easily be extended and tested!
Routes • Routes are the entry point to the application • In Web Forms a file on disk is the entry point • Routes are defined at startup • Routes define a parameterized URL, a route handler, default values, constraints, and an optional name • Routing works both ways • Route data can be built from a URL • A URL can be built from route data
Route Handlers • Created by the current route • Single purpose: create an IHttpHandler • In the case of MVC it creates an instance of an MvcHandler
Controller Factory • Single responsibility: Given the route data, create a controller • The default controller factory automagically finds your controllers based on convention by scanning loaded assemblies looking for classes that… • Are public • Name ends with ‘Controller’ • Are not abstract • Is assignable to IController
Controller & Actions • The controller is little more than a class containing actions • Actions are all public methods on a controller (usually) • Actions should return an ActionResult • Controllers and Actions can be decorated with filters to customize this step • Validation, authorization, error handling, output caching, ignoring a public method, & filtering by HTTP method come in the box to name a few • Extensible by implementing some basic interfaces
View Engines & Views • The View Engine takes the data from the Action and renders the output • View Engines are pluggable • The default View Engine uses Web Forms • Uses the rendering engine only • No ViewState • View files are found via convention • Unless specified, the ‘Index’ Action of the ‘Home’ Controller will start looking for the View at ~/Views/Home/Index.aspx
MVC vs. Web Forms or • Both get you from A to B, but MVC is a much more raw, hands-on experience • Web Forms abstracts much of web development from the programmer • After a slight learning curve you won’t look back
Where do I get it? • ASP.NET MVC v1 available as an add-on for Visual Studio 2008 • Requires .NET 3.5 SP1 • ASP.NET MVC v2 is currently in RC2 and will likely ship very soon • Will work both with .NET 3.5 SP1 and .NET 4.0 • Will come ‘in the box’ with Visual Studio 2010
Resources • http://www.asp.net/mvc/ • http://aspnet.codeplex.com/ • http://stackoverflow.com/