ASP NET MVC http www asp netmvc Scott

  • Slides: 41
Download presentation
ASP. NET MVC http: //www. asp. net/mvc Scott Guthrie scottgu@microsoft. com http: //weblogs. asp.

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

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 Web. Forms improvements coming

Goals Maintain Clean Separation of Concerns Enable highly maintainable applications Enable Easy Unit Testing

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

Goals Enable clean URLs and HTML SEO and REST friendly URL structures Great integration

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.

Current Status ASP. NET MVC Preview 5 Currently Out Beta release in a few

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

MVC Model View Controller

MVC Model View Controller

How ASP. NET MVC Works http: //myserver. com/Products/ Browser http: //myserver. com/Products/Edit/5 Products. Controller

How ASP. NET MVC Works http: //myserver. com/Products/ Browser http: //myserver. com/Products/Edit/5 Products. Controller (Controller) Product (Model) a iew. Dat V Web Server List. aspx (View) Edit. aspx (View) SQL

Demo Building a Product Catalog

Demo Building a Product Catalog

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

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

Demo Handling Updates

Demo Handling Updates

Filter Attributes Enable custom behavior to be added to Controllers and Controller actions Examples:

Filter Attributes Enable custom behavior to be added to Controllers and Controller actions Examples: [Output. Cache] [Authorize] [Handle. Error]

Demo Attributes

Demo Attributes

Custom URL Routing Can setup custom URL routing rules within the Register. Routes() method

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

Demo Custom Routing

Demo Custom Routing

AJAX Helpers Built-in AJAX helper object within views Ajax. Form() Ajax. Action. Link() Ajax.

AJAX Helpers Built-in AJAX helper object within views Ajax. Form() Ajax. Action. Link() Ajax. Route. Link() Automatically emit client-side Java. Script Use ASP. NET AJAX library by default Pluggable to use j. Query, Prototype, or other AJAX libraries as well

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

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

Summary A new option for ASP. NET. More control over your <html/> and URLs

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

© 2008 Microsoft Corporation. All rights reserved. Microsoft, Windows Vista and other product names

© 2008 Microsoft Corporation. All rights reserved. Microsoft, 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.

Interfaces and Testing Mockable Intrinsics Http. Context. Base, Http. Response. Base, Http. Request. Base

Interfaces and Testing Mockable Intrinsics Http. Context. Base, Http. Response. Base, Http. Request. Base Extensibility IController. Factory IRoute. Handler View. Engine. Base

Testing Controller Actions No requirement to test within ASP. NET runtime. Can mock parts

Testing Controller Actions No requirement to test within ASP. NET runtime. Can mock parts of runtime you want to fake [Test. Method] public void Show. Posts. Display. Post. View() { Blog. Controller controller = new Blog. Controller(…); var result = controller. Show. Post(2) as View. Result; Assert. Is. Not. Null(result); Assert. Are. Equal(result. View. Data[“Message”], “Hello”); }

Common Questions Should I use Web. Forms or MVC? Difference between MVC and MVP?

Common Questions Should I use Web. Forms 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?

Even More Detail – Request Flow Request • You can futz at each step

Even More Detail – Request Flow Request • You can futz at each step View Controller in the process Engine HTTP Routing Http Handler View Route Handle r Respons e

Extensibility Views Controllers Models Routes …are all Pluggable

Extensibility Views Controllers Models Routes …are all Pluggable

View. Engine. Base View Engines render output You get Web. Forms by default Can

View. Engine. Base View Engines render output You get Web. Forms 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.

View Engine Base Class View. Engine. Base public abstract class View. Engine. Base {

View Engine Base Class View. Engine. Base public abstract class View. Engine. Base { public abstract void Render. View(View. Context view. Context); }

NHaml – Extreme Custom Views <%@ Page Language="C#" Master. Page. File="~/Views/Shared/Site. Master" Auto. Event.

NHaml – Extreme Custom Views <%@ Page Language="C#" Master. Page. File="~/Views/Shared/Site. Master" Auto. Event. Wireup="true" Code. Behind="List. aspx" Inherits="Mvc. Application 5. Views. Products. List" Title="Products" %> <asp: Content. Place. Holder. ID="Main. Content. Place. Holder" runat="server"> <h 2><%= View. Data. Category. Name %></h 2> <ul> <% foreach (var product in View. Data. Products) { %> <li> <%= product. Product. Name %> <div class="editlink"> (<%= Html. Action. Link("Edit", new { Action="Edit", ID=product. Product. ID })%>) </div> </li> <% } %> </ul> <%= Html. Action. Link("Add New Product", new { Action="New" }) %> </asp: Content>

NHaml – Extreme Custom Views %h 2= View. Data. Category. Name %ul - foreach

NHaml – Extreme Custom Views %h 2= View. Data. Category. Name %ul - foreach (var product in View. Data. Products) %li = product. Product. Name. editlink = Html. Action. Link("Edit", new { Action="Edit", ID=product. Product. ID }) = Html. Action. Link("Add New Product", new { Action="New" })

MVC APPENDIX

MVC APPENDIX

Controller Base Controller Class Basic Functionality most folks will use IController Interface Ultimate Control

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

Basic Controller Handling Scenarios, Goals and Design URLs route to controller “actions”, not pages

Basic Controller Handling Scenarios, Goals and Design URLs route to controller “actions”, not pages – mark actions in Controller executes logic, chooses view. All public methods are accessible public void Show. Post(int id) { Post p = Post. Repository. Get. Post. By. Id(id); if (p != null) { Render. View("showpost", p); } else { Render. View("nosuchpost", id); } }

Controller Base Class public class Controller : IController { … protected virtual void Execute(Controller.

Controller Base Class public class Controller : IController { … protected virtual void Execute(Controller. Context controller. Context); protected virtual void Handle. Unknown. Action(string action. Name); protected virtual bool Invoke. Action(string action. Name); protected virtual void Invoke. Action. Method(Method. Info method. Info); protected virtual bool On. Error(string action. Name, Method. Info method. Info, Exception exception); protected virtual void On. Action. Executed(Filter. Executed. Context filter. Context); protected virtual bool On. Action. Executing(Filter. Executed. Context filter. Context); protected virtual void Redirect. To. Action(object values); protected virtual void Render. View(string view. Name, string master. Name, object view. Data); }

Controller – Regular APIs public class Controller : IController { … protected virtual void

Controller – Regular APIs public class Controller : IController { … protected virtual void Execute(Controller. Context controller. Context); protected virtual void Handle. Unknown. Action(string action. Name); protected virtual bool Invoke. Action(string action. Name); protected virtual void Invoke. Action. Method(Method. Info method. Info); protected virtual bool On. Error(string action. Name, Method. Info method. Info, Exception exception); protected virtual void On. Action. Executed(Filter. Executed. Context filter. Context); protected virtual bool On. Action. Executing(Filter. Executed. Context filter. Context); protected virtual void Redirect. To. Action(object values); protected virtual void Render. View(string view. Name, string master. Name, object view. Data); }

Controller – Customization APIs public class Controller : IController { … protected virtual void

Controller – Customization APIs public class Controller : IController { … protected virtual void Execute(Controller. Context controller. Context); protected virtual void Handle. Unknown. Action(string action. Name); protected virtual bool Invoke. Action(string action. Name); protected virtual void Invoke. Action. Method(Method. Info method. Info); protected virtual bool On. Error(string action. Name, Method. Info method. Info, Exception exception); protected virtual void On. Action. Executed(Filter. Executed. Context filter. Context); protected virtual bool On. Action. Executing(Filter. Executed. Context filter. Context); protected virtual void Redirect. To. Action(object values); protected virtual void Render. View(string view. Name, string master. Name, object view. Data); }

Controller – Test Hooks public class Controller : IController { … protected virtual void

Controller – Test Hooks public class Controller : IController { … protected virtual void Execute(Controller. Context controller. Context); protected virtual void Handle. Unknown. Action(string action. Name); protected virtual bool Invoke. Action(string action. Name); protected virtual void Invoke. Action. Method(Method. Info method. Info); protected virtual bool On. Error(string action. Name, Method. Info method. Info, Exception exception); protected virtual void On. Action. Executed(Filter. Executed. Context filter. Context); protected virtual bool On. Action. Executing(Filter. Executed. Context filter. Context); protected virtual void Redirect. To. Action(object values); protected virtual void Render. View(string view. Name, string master. Name, object view. Data); }

Basic Views Scenarios, Goals and Design: Are for rendering/output. Pre-defined and extensible rendering helpers

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

View. Engine. Base View Engines render output You get Web. Forms by default Can

View. Engine. Base View Engines render output You get Web. Forms 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.

View Engine Base Class View. Engine. Base public abstract class View. Engine. Base {

View Engine Base Class View. Engine. Base public abstract class View. Engine. Base { public abstract void Render. View(View. Context view. Context); }

NHaml – Extreme Custom Views <%@ Page Language="C#" Master. Page. File="~/Views/Shared/Site. Master" Auto. Event.

NHaml – Extreme Custom Views <%@ Page Language="C#" Master. Page. File="~/Views/Shared/Site. Master" Auto. Event. Wireup="true" Code. Behind="List. aspx" Inherits="Mvc. Application 5. Views. Products. List" Title="Products" <asp: Content. Place. Holder. ID="Main. Content. Place. Holder" runat="server"> <h 2><%= View. Data. Category. Name %></h 2> <ul> <% foreach (var product in View. Data. Products) { %> <li> <%= product. Product. Name %> <div class="editlink"> (<%= Html. Action. Link("Edit", new { Action="Edit", ID=product. Product. ID })%>) </div> </li> <% } %> </ul> <%= Html. Action. Link("Add New Product", new { Action="New" }) %> </asp: Content>

NHaml – Extreme Custom Views %h 2= View. Data. Category. Name %ul - foreach

NHaml – Extreme Custom Views %h 2= View. Data. Category. Name %ul - foreach (var product in View. Data. Products) %li = product. Product. Name. editlink = Html. Action. Link("Edit", new { Action="Edit", ID=product. Product. ID }) = Html. Action. Link("Add New Product", new { Action="New" })

Controller Factory Scenarios, Goals and Design: Hook creation of controller instance Dependency Injection. Object

Controller Factory Scenarios, Goals and Design: Hook creation of controller instance Dependency Injection. Object Interception. public interface IController. Factory { IController Create. Controller(Request. Context context, string controller. Name); } protected void Application_Start(object s, Event. Args e) { Controller. Builder. Current. Set. Controller. Factory( typeof(My. Controller. Factory)); }

View Engine Scenarios, Goals and Design: Mock out views for testing Replace ASPX with

View Engine Scenarios, Goals and Design: Mock out views for testing Replace ASPX with other technologies public interface IView. Engine { void Render. View(View. Context context); } Inside controller class: this. View. Engine = new Xml. View. Engine(. . . ); Render. View("foo", my. Data);