1 / 41

ASP.NET MVC http://www.asp.net/mvc

ASP.NET MVC http://www.asp.net/mvc. Scott Guthrie scottgu@microsoft.com http://weblogs.asp.net/scottgu. ASP.NET MVC. A new option for ASP.NET Easily testable and TDD friendly “Closer to the Metal” option HTTP programming model surfaced More control over <html/> and URLs

hunter
Download Presentation

ASP.NET MVC http://www.asp.net/mvc

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 MVChttp://www.asp.net/mvc Scott Guthrie scottgu@microsoft.com http://weblogs.asp.net/scottgu

  2. ASP.NET MVC • A new option for ASP.NET • Easily testable and TDD friendly • “Closer to the Metal” option • HTTP programming model surfaced • More control over <html/> and URLs • Not for everyone (car vs. motorcycle) • Lots of WebForms improvements coming

  3. Goals • Maintain Clean Separation of Concerns • Enable highly maintainable applications • Enable Easy Unit Testing • Support Red/Green TDD • Extensible and Pluggable • Support replacing any component of system

  4. Goals • Enable clean URLs and HTML • SEO and REST friendly URL structures • Great integration within ASP.NET • All the same providers still work • Membership, Session, Caching, etc.

  5. Current Status • ASP.NET MVC Preview 5 Currently Out • Beta release in a few weeks • Final V1 by end of the year • Production Deployment Supported Now

  6. MVC

  7. How ASP.NET MVC Works http://myserver.com/Products/ Web Server Browser http://myserver.com/Products/Edit/5 List.aspx (View) ViewData ProductsController (Controller) Edit.aspx (View) SQL Product (Model)

  8. DemoBuilding a Product Catalog

  9. Understanding Form Interactions URL: GET: /Products/Edit/5 ProductsController (Controller) POST: /Products/Edit/5 SQL Product (Model)

  10. DemoHandling Updates

  11. Filter Attributes • Enable custom behavior to be added to Controllers and Controller actions • Examples: • [OutputCache] • [Authorize] • [HandleError]

  12. DemoAttributes

  13. Custom URL Routing • Can setup custom URL routing rules within the RegisterRoutes() method in Global.asax • Enables very flexible mapping and route rules: • Default parameters • Constraints • Named routes • Wildcard “catch-all” routes

  14. DemoCustom Routing

  15. AJAX Helpers • Built-in AJAX helper object within views • Ajax.Form() • Ajax.ActionLink() • Ajax.RouteLink() • Automatically emit client-side JavaScript • Use ASP.NET AJAX library by default • Pluggable to use jQuery, Prototype, or other AJAX libraries as well

  16. More Extensibility Goodness • Custom Route Rules • Controller Factories • Custom IController implementations • Custom View Engines

  17. Summary • A new option for ASP.NET. • More control over your <html/> and URLs • More easily testable approach • Not for everyone – only use it if you want to • Shipping later this year

  18. © 2008 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

  19. Interfaces and Testing • MockableIntrinsics • HttpContextBase, HttpResponseBase, HttpRequestBase • Extensibility • IController • IControllerFactory • IRouteHandler • ViewEngineBase

  20. Testing Controller Actions • No requirement to test within ASP.NET runtime. • Can mock parts of runtime you want to fake [TestMethod] public void ShowPostsDisplayPostView() { BlogController controller = new BlogController(…); var result = controller.ShowPost(2) as ViewResult; Assert.IsNotNull(result); Assert.AreEqual(result.ViewData[“Message”], “Hello”); }

  21. Common Questions • Should I use WebForms or MVC? • Difference between MVC and MVP? • Do I have to write <%= %> code in my view? • What about AJAX? • How fast/scalable is it? • When will it ship?

  22. Even More Detail – Request Flow • You can futz at each step in the process

  23. Extensibility • Views • Controllers • Models • Routes …are all Pluggable

  24. ViewEngineBase • View Engines render output • You get WebForms by default • Can implement your own • MVCContrib has ones for Brail, Nvelocity • NHaml is an interesting one to watch • View Engines can be used to • Offer new DSLs to make HTML easier • Generate totally different mime/types • Images, RSS, JSON, XML, OFX, VCards, whatever.

  25. View Engine Base Class • ViewEngineBase public abstract class ViewEngineBase { public abstract void RenderView(ViewContextviewContext); }

  26. NHaml – Extreme Custom Views <%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" AutoEventWireup="true" CodeBehind="List.aspx" Inherits="MvcApplication5.Views.Products.List" Title="Products" %> <asp:ContentContentPlaceHolderID="MainContentPlaceHolder" runat="server"> <h2><%= ViewData.CategoryName %></h2> <ul> <% foreach (var product in ViewData.Products) { %> <li> <%= product.ProductName %> <div class="editlink"> (<%= Html.ActionLink("Edit", new { Action="Edit", ID=product.ProductID })%>) </div> </li> <% } %> </ul> <%= Html.ActionLink("Add New Product", new { Action="New" }) %> </asp:Content>

  27. NHaml – Extreme Custom Views %h2= ViewData.CategoryName%ul - foreach (var product in ViewData.Products) %li = product.ProductName .editlink = Html.ActionLink("Edit", new { Action="Edit", ID=product.ProductID }) = Html.ActionLink("Add New Product", new { Action="New" })

  28. MVC Appendix

  29. Controller • Base Controller Class • Basic Functionality most folks will use • IController Interface • Ultimate Control for the Control Freak • IControllerFactory • For plugging in your own stuff (IOC, etc)

  30. Basic Controller Handling • Scenarios, Goals and Design • URLs route to controller “actions”, not pages – mark actions in Controller. • Controller executes logic, chooses view. • All public methods are accessible public void ShowPost(int id) { Post p = PostRepository.GetPostById(id); if (p != null) { RenderView("showpost", p); } else { RenderView("nosuchpost", id); } }

  31. Controller Base Class public class Controller : IController { … protected virtual void Execute(ControllerContextcontrollerContext); protected virtual void HandleUnknownAction(string actionName); protected virtual boolInvokeAction(string actionName); protected virtual void InvokeActionMethod(MethodInfomethodInfo); protected virtual boolOnError(string actionName, MethodInfomethodInfo, Exception exception); protected virtual void OnActionExecuted(FilterExecutedContextfilterContext); protected virtual boolOnActionExecuting(FilterExecutedContextfilterContext); protected virtual void RedirectToAction(object values); protected virtual void RenderView(string viewName, string masterName, object viewData); }

  32. Controller – Regular APIs public class Controller : IController { … protected virtual void Execute(ControllerContextcontrollerContext); protected virtual void HandleUnknownAction(string actionName); protected virtual boolInvokeAction(string actionName); protected virtual void InvokeActionMethod(MethodInfomethodInfo); protected virtual boolOnError(string actionName, MethodInfomethodInfo, Exception exception); protected virtual void OnActionExecuted(FilterExecutedContextfilterContext); protected virtual boolOnActionExecuting(FilterExecutedContextfilterContext);protected virtual void RedirectToAction(object values); protected virtual void RenderView(string viewName, string masterName, object viewData); }

  33. Controller – Customization APIs public class Controller : IController { … protected virtual void Execute(ControllerContextcontrollerContext); protected virtual void HandleUnknownAction(string actionName); protected virtual boolInvokeAction(string actionName); protected virtual void InvokeActionMethod(MethodInfomethodInfo); protected virtual boolOnError(string actionName, MethodInfomethodInfo, Exception exception); protected virtual void OnActionExecuted(FilterExecutedContextfilterContext); protected virtual boolOnActionExecuting(FilterExecutedContextfilterContext); protected virtual void RedirectToAction(object values); protected virtual void RenderView(string viewName, string masterName, object viewData); }

  34. Controller – Test Hooks public class Controller : IController { … protected virtual void Execute(ControllerContextcontrollerContext); protected virtual void HandleUnknownAction(string actionName); protected virtual boolInvokeAction(string actionName); protected virtual void InvokeActionMethod(MethodInfomethodInfo); protected virtual boolOnError(string actionName, MethodInfomethodInfo, Exception exception); protected virtual void OnActionExecuted(FilterExecutedContextfilterContext); protected virtual boolOnActionExecuting(FilterExecutedContextfilterContext); protected virtual void RedirectToAction(object values); protected virtual void RenderView(string viewName, string masterName, object viewData); }

  35. Basic Views • Scenarios, Goals and Design: • Are for rendering/output. • Pre-defined and extensible rendering helpers • Can use .ASPX, .ASCX, .MASTER, etc. • Can replace with other view technologies: • Template engines (NVelocity, Brail, …). • Output formats (images, RSS, JSON, …). • Mock out for testing. • Controller sets data on the View • Loosely typed or strongly typed data

  36. ViewEngineBase • View Engines render output • You get WebForms by default • Can implement your own • MVCContrib has ones for Brail, Nvelocity • NHaml is an interesting one to watch • View Engines can be used to • Offer new DSLs to make HTML easier to write • Generate totally different mime/types • Images • RSS, JSON, XML, OFX, etc. • VCards, whatever.

  37. View Engine Base Class • ViewEngineBase public abstract class ViewEngineBase { public abstract void RenderView(ViewContextviewContext); }

  38. NHaml – Extreme Custom Views <%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" AutoEventWireup="true" CodeBehind="List.aspx" Inherits="MvcApplication5.Views.Products.List" Title="Products" %> <asp:ContentContentPlaceHolderID="MainContentPlaceHolder" runat="server"> <h2><%= ViewData.CategoryName %></h2> <ul> <% foreach (var product in ViewData.Products) { %> <li> <%= product.ProductName %> <div class="editlink"> (<%= Html.ActionLink("Edit", new { Action="Edit", ID=product.ProductID })%>) </div> </li> <% } %> </ul> <%= Html.ActionLink("Add New Product", new { Action="New" }) %> </asp:Content>

  39. NHaml – Extreme Custom Views %h2= ViewData.CategoryName%ul - foreach (var product in ViewData.Products) %li = product.ProductName .editlink = Html.ActionLink("Edit", new { Action="Edit", ID=product.ProductID }) = Html.ActionLink("Add New Product", new { Action="New" })

  40. Controller Factory • Scenarios, Goals and Design: • Hook creation of controller instance • Dependency Injection. • Object Interception. public interface IControllerFactory { IControllerCreateController(RequestContext context, string controllerName); } protected void Application_Start(object s, EventArgs e) { ControllerBuilder.Current.SetControllerFactory( typeof(MyControllerFactory)); }

  41. View Engine • Scenarios, Goals and Design: • Mock out views for testing • Replace ASPX with other technologies public interface IViewEngine { void RenderView(ViewContext context); } Inside controller class: this.ViewEngine = new XmlViewEngine(...); RenderView("foo", myData);

More Related