CTTDNUG ASP.NET MVC

47
Introduction to ASP.NET MVC Barry Gervin, Partner [email protected] @bgervin ObjectSharp.com/blogs/Barry

description

CTTDNUG ASP.NET MVC

Transcript of CTTDNUG ASP.NET MVC

Page 1: CTTDNUG ASP.NET MVC

Introduction to ASP.NET MVCBarry Gervin, [email protected]@bgervinObjectSharp.com/blogs/Barry

Page 2: CTTDNUG ASP.NET MVC

Objectives

• Why do we need something else?– What’s wrong with Web Forms?

• “How To” build ASP.NET MVC– How does this compare? Help?

• What should I beware of?

Page 3: CTTDNUG ASP.NET MVC

Agenda

• WebForms• MVC• ASP.NET MVC– Tenets

• WebForms Vs. MVC• Walkthrough Real Application• Testing MVC• Extensibility

Page 4: CTTDNUG ASP.NET MVC

ASP.NET WEB FORMS

Page 5: CTTDNUG ASP.NET MVC

ASP.NET Then…

CachingCaching ModulesModules

HandlersHandlersIntrinsicsIntrinsics

PagesPages ControlsControls

GlobalizationGlobalization

ProfileProfile

Master PagesMaster Pages

MembershipMembershipRolesRoles

Etc.Etc.

ASP.NET

One web applicationframework to rule them all…

Page 6: CTTDNUG ASP.NET MVC

WebFormsASPX Page

User/Custom/ServerControls

Master Page

User/Custom/ServerControls

Model

Page 7: CTTDNUG ASP.NET MVC

QUICK DEMOPages, Events and Postbacks

Page 8: CTTDNUG ASP.NET MVC

Page Controller

ASPX Page

ASP.NET

Response

Request

Page 9: CTTDNUG ASP.NET MVC

Master Page

Control

Control

No real role responsibility…

UIPresentation LogicBusiness LogicData Access

Who does what?How and when?

Control

ControlPage

Control

Control

Control

Control

Page 10: CTTDNUG ASP.NET MVC

Control abstractions can be negative…

Page 11: CTTDNUG ASP.NET MVC

It isn't easy enough to test…It isn't easy enough to test…

Logic UI

Page 12: CTTDNUG ASP.NET MVC

WebForms are great, but options are

good…

Page 13: CTTDNUG ASP.NET MVC

ASP.NET MVC

Page 14: CTTDNUG ASP.NET MVC

ASP.NET Now…

ASP.NETDynamic Data

ASP.NETWebForms

ASP.NETMVC

Presentation

RuntimeASP.NET

Core

Page 15: CTTDNUG ASP.NET MVC

So how does ASP.NET MVC

differ?

Page 16: CTTDNUG ASP.NET MVC

MVC = Model-View-Controller

ControllerController(Input)(Input)

ModelModel(Logic)(Logic)

ViewView(Presentation)(Presentation)

Separation of concerns!

Page 17: CTTDNUG ASP.NET MVC

ASP.NET MVC

Model

ASPX Page

User/Custom/ServerControls

Master Page

User/Custom/ServerControls

Controller

Page 18: CTTDNUG ASP.NET MVC

How does MVC look?

Request

View

Controller

Response

ControllerHandles input(HTTP requests)

ViewVisually representsthe model

Page 19: CTTDNUG ASP.NET MVC

Framework Goals

• Alternative

• Frictionless Testability

• Tight control over <markup>

• Leverage the benefits of ASP.NET

• Routable Urls to Controllers to Views

• Conventions and guidance

• Modular and Extensible

Page 20: CTTDNUG ASP.NET MVC

Project Structure

Routes

Views/Master

Views/Master

Models

Controllers

Page 21: CTTDNUG ASP.NET MVC

ASP.NET MVC DEMORoutes, Controllers & Views

Page 22: CTTDNUG ASP.NET MVC

ASP.NET MVC doesn’t have…

• Postbacks• View state• Control state• Server-side form• Page/Control lifecycle

Page 23: CTTDNUG ASP.NET MVC

ASP.NET MVC still has…• Web designer• Master pages• User controls• Membership/Roles/Profile• Globalization• Caching• HTTP intrinsics:– HttpContext– HttpRequest– HttpResponse– Etc.

Page 24: CTTDNUG ASP.NET MVC

Routing

Page 25: CTTDNUG ASP.NET MVC

mysite.com/object/action/parameter

Page 26: CTTDNUG ASP.NET MVC

Clean URLs

Don’t settle for…

/Products.aspx?CategoryID=123

When you can easily have…

/Product/Puppies

Or whatever else makes sense…

Page 27: CTTDNUG ASP.NET MVC

protected void Application_Start(object sender, EventArgs e) {

RouteTable.Routes.Add("SearchRoute", new Route("search/{searchterm}", new WebFormRouteHandler("~/search.aspx")));

RouteTable.Routes.Add("UserRoute", new Route("users/{username}", new WebFormRouteHandler("~/users.aspx")));

} .... protected void Page_Load(object sender, EventArgs e) {

string searchterm= Page.RouteData.Values["searchterm"] as string; Response.Write(searchterm); }... <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="<%$RouteUrl:SearchTerm=Bob%>">Link to search</asp:HyperLink>

Routing

Page 28: CTTDNUG ASP.NET MVC

Controller Conventions• Controller– Must…

• Be suffixed with “Controller”• Implement IController (or inherit from Controller)

• Action– Must…

• Be Public• Return ActionResult or void

– Can’t…• Be generic• Have a NonActionAttribute• Have out/ref parameters

Page 29: CTTDNUG ASP.NET MVC

REAL WORLD WALKTHROUGHSayITFirst.ca

Page 30: CTTDNUG ASP.NET MVC

Extensibility Pipeline

ControllerBuilder

ControllerFactory

Controller

ViewEngine

ViewLocator

View

ControllerActionInvoker

ActionResult*

ActionFilters

* ViewResult

Page 31: CTTDNUG ASP.NET MVC

Action Results

• ActionResult– ContentResult– EmptyResult– JsonResult– RedirectResult– RedirectToRouteResult– ViewResult

ControllerController

ActionInvoker

ActionResult

Page 32: CTTDNUG ASP.NET MVC

Action Filters• IActionFilter– Controller– ActionFilterAttribute

1. OnActionExecuting2. [Action Method Executed]3. OnActionExecuted4. OnResultExecuting5. [Action Result Executed]6. OnResultExecuted

Page 33: CTTDNUG ASP.NET MVC

Controller Factory

• IControllerFactory– DefaultControllerFactory

ControllerBuilder

ControllerFactory

Controller

Page 34: CTTDNUG ASP.NET MVC

View Engine

• IViewEngine– WebFormViewEngine

• IViewLocator– ViewLocator

• WebFormViewLocator

ViewEngine

ViewLocator

ViewActionResult*

Page 35: CTTDNUG ASP.NET MVC

ASP.NET MVC 2

• A good number of new features including:– Client-side validation based on the model’s

validation metadata– Areas to better organize an application– Model validation providers to hook in alternative

validation logic when model binding– Metadata providers to allow for alternative

sources of metadata for model objects

Page 36: CTTDNUG ASP.NET MVC

Microsoft.com/web

Page 37: CTTDNUG ASP.NET MVC

Web Platform Installer

Page 38: CTTDNUG ASP.NET MVC

NerdDinner (nerddinner.com)

Page 39: CTTDNUG ASP.NET MVC

Professional ASP.NET MVC 1.0,

a.k.a. The “Gang of Foreheads” Book

Page 40: CTTDNUG ASP.NET MVC

ASP.NET MVC in Action

@ManningBooks

Page 41: CTTDNUG ASP.NET MVC

Pro ASP.NET MVC Framework

(Highest rating on Amazon.com)

Page 42: CTTDNUG ASP.NET MVC

The Official ASP.NET MVC SiteASP.NET/MVC

Page 43: CTTDNUG ASP.NET MVC

You’ve Been Haacked (Phil Haack) Haacked.com

Page 44: CTTDNUG ASP.NET MVC

Scott Hanselman’s BlogHanselman.com

Page 45: CTTDNUG ASP.NET MVC

ObjectSharp.com

Page 46: CTTDNUG ASP.NET MVC

ResourcesMicrosoft Starting Points•http://microsoft.com/web•http://asp.net/mvc•http://codeplex.com/MVCContrib•http://aspnet.codeplex.com

Community Starting Points•http://channel9.msdn.com/tags/asp.net+mvc•http://haacked.com/•http://stackoverflow.com

Page 47: CTTDNUG ASP.NET MVC

Summary

• Web Forms vs. MVC• 101 How-To• Real World ASP.NET MVC• Testability