asp.net webforms vs. asp.net mvc - michiel van otegemmichiel.vanotegem.nl/content/binary/webforms vs...

23
ASP.NET WebForms vs. ASP.NET MVC Introducing MVC to WebForms Developers

Upload: others

Post on 21-Jul-2020

27 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ASP.NET WebForms vs. ASP.NET MVC - Michiel van Otegemmichiel.vanotegem.nl/content/binary/WebForms vs MVC.pdf · Request Response BeginRequest AuthenticateRequest AuthorizeRequest

ASP.NET WebForms

vs.

ASP.NET MVC

Introducing MVC to WebForms Developers

Page 2: ASP.NET WebForms vs. ASP.NET MVC - Michiel van Otegemmichiel.vanotegem.nl/content/binary/WebForms vs MVC.pdf · Request Response BeginRequest AuthenticateRequest AuthorizeRequest

Why WebForms?

• Reaction to ASP “Classic” disadvantages

– Code EVERYTHING by hand

– Loosely Typed, bad error handling, no memory mgmt

• Make building web apps more like desktop apps

– Abstract away HTTP

– Object Oriented and Event-based, rather than script

– Strongly Typed code

• Created around 2000

Page 3: ASP.NET WebForms vs. ASP.NET MVC - Michiel van Otegemmichiel.vanotegem.nl/content/binary/WebForms vs MVC.pdf · Request Response BeginRequest AuthenticateRequest AuthorizeRequest

ASP.NET Features

• Page & Object caching

• Authentication, Authorization & Membership

• Application State & Session State

• Master Pages & Themes

• Provider Model based functionality

• Virtual File System

• And more …

Page 4: ASP.NET WebForms vs. ASP.NET MVC - Michiel van Otegemmichiel.vanotegem.nl/content/binary/WebForms vs MVC.pdf · Request Response BeginRequest AuthenticateRequest AuthorizeRequest

ASP.NET Pipeline

HTTP Pipeline

HTTP Handler

Request Response

BeginRequest

AuthenticateRequest

AuthorizeRequest

ResolveRequestCache

AcquireRequestState

PreRequestHandlerExecute PostRequestHandlerExecute

ReleaseRequestState

UpdateRequestCache

EndRequest

Page 5: ASP.NET WebForms vs. ASP.NET MVC - Michiel van Otegemmichiel.vanotegem.nl/content/binary/WebForms vs MVC.pdf · Request Response BeginRequest AuthenticateRequest AuthorizeRequest

WebForms Basics

• Object oriented Page with an event based lifecycle

• Page can contain server controls

– Interactive objects rendering part of a page

– Can have events like Click, SelectedIndexChanged etc.

• Page contains ViewState

– Hidden data to maintain the illusion of state

– If managed improperly can become very big (> 1 MB)

Page 6: ASP.NET WebForms vs. ASP.NET MVC - Michiel van Otegemmichiel.vanotegem.nl/content/binary/WebForms vs MVC.pdf · Request Response BeginRequest AuthenticateRequest AuthorizeRequest

WebForms

Request Handling

Request Response

HTTP Module(s)

Page Handler

Business Logic

Page (.aspx)

Page 7: ASP.NET WebForms vs. ASP.NET MVC - Michiel van Otegemmichiel.vanotegem.nl/content/binary/WebForms vs MVC.pdf · Request Response BeginRequest AuthenticateRequest AuthorizeRequest

Alter State an

d R

end

er Page

(Re)

Bu

ild P

age

Stat

eWebForms

Page Lifecycle

PreInit

Init

PreRender

PreRenderComplete

Render

InitComplete

PreLoad

LoadComplete

Load

SaveState

SaveStateComplete

Control Events

Request Response

Page 8: ASP.NET WebForms vs. ASP.NET MVC - Michiel van Otegemmichiel.vanotegem.nl/content/binary/WebForms vs MVC.pdf · Request Response BeginRequest AuthenticateRequest AuthorizeRequest

WebForms App

Page 9: ASP.NET WebForms vs. ASP.NET MVC - Michiel van Otegemmichiel.vanotegem.nl/content/binary/WebForms vs MVC.pdf · Request Response BeginRequest AuthenticateRequest AuthorizeRequest

MVC

• Design Pattern Invented in 1979 for Smalltalk

• Used in various technologies for the last 30 years

• Used more and more on the web

– e.g. Struts, Ruby on Rails, Django, and now in ASP.NET

• Separates UI from logic through shared data

– Enables different UI for same functionality

– Makes testing UI easier

Page 10: ASP.NET WebForms vs. ASP.NET MVC - Michiel van Otegemmichiel.vanotegem.nl/content/binary/WebForms vs MVC.pdf · Request Response BeginRequest AuthenticateRequest AuthorizeRequest

MVC Overview

Controller

ModelView

Page 11: ASP.NET WebForms vs. ASP.NET MVC - Michiel van Otegemmichiel.vanotegem.nl/content/binary/WebForms vs MVC.pdf · Request Response BeginRequest AuthenticateRequest AuthorizeRequest

ASP.NET MVC

• New ASP.NET project type (WebForms is not gone!)

– MVC 1.0 extension on .NET 3.5 SP1

– MVC 2.0 part of .NET 4.0 and Visual Studio 2010

• Excellent for (public) web applications/sites

– 100% control over HTML (or other output)

– (Search Engine) Friendly URLs

– No (illusion of ) state as in WebForms

• Easy to test, comes with unit tests by default

Page 12: ASP.NET WebForms vs. ASP.NET MVC - Michiel van Otegemmichiel.vanotegem.nl/content/binary/WebForms vs MVC.pdf · Request Response BeginRequest AuthenticateRequest AuthorizeRequest

View

MVC

Request Handling

Request Response

Module(s)

MvcHandler(Routing)

Business Logic

Controller

Model

Page 13: ASP.NET WebForms vs. ASP.NET MVC - Michiel van Otegemmichiel.vanotegem.nl/content/binary/WebForms vs MVC.pdf · Request Response BeginRequest AuthenticateRequest AuthorizeRequest

WebForms

Request Handling

Request Response

HTTP Module(s)

Page Handler

Business Logic

Page (.aspx)

Page 14: ASP.NET WebForms vs. ASP.NET MVC - Michiel van Otegemmichiel.vanotegem.nl/content/binary/WebForms vs MVC.pdf · Request Response BeginRequest AuthenticateRequest AuthorizeRequest

Routing

• Routing Engine directs requests to proper controller

– Routes must be defined at application start

– Maps URLs to controller, action, and parameters

– Uses naming convention: Trip TripController

• Routes are evaluated in order, first match “wins”

• Routing implemented by System.Web.Routing

– Available since .NET 3.5 SP1, not MVC specific

Page 15: ASP.NET WebForms vs. ASP.NET MVC - Michiel van Otegemmichiel.vanotegem.nl/content/binary/WebForms vs MVC.pdf · Request Response BeginRequest AuthenticateRequest AuthorizeRequest

Routing

Routes.MapRoute(

"Default",

"{controller}/{action}/{id}",

new { controller = "Home",

action = "Index",

id = "" });

C#

Page 16: ASP.NET WebForms vs. ASP.NET MVC - Michiel van Otegemmichiel.vanotegem.nl/content/binary/WebForms vs MVC.pdf · Request Response BeginRequest AuthenticateRequest AuthorizeRequest

Routing Demo

Page 17: ASP.NET WebForms vs. ASP.NET MVC - Michiel van Otegemmichiel.vanotegem.nl/content/binary/WebForms vs MVC.pdf · Request Response BeginRequest AuthenticateRequest AuthorizeRequest

MVC Controller

• Class inheriting System.Web.Mvc.Controller

• Public methods are Actions called from Routing

• Usually builds a Model to be used later

– Can use ViewData (State Bag) for model/state

– Model can also be passed directly strongly typed

• Usually returns ActionResult telling where to go next

Page 18: ASP.NET WebForms vs. ASP.NET MVC - Michiel van Otegemmichiel.vanotegem.nl/content/binary/WebForms vs MVC.pdf · Request Response BeginRequest AuthenticateRequest AuthorizeRequest

Controller Demo

Page 19: ASP.NET WebForms vs. ASP.NET MVC - Michiel van Otegemmichiel.vanotegem.nl/content/binary/WebForms vs MVC.pdf · Request Response BeginRequest AuthenticateRequest AuthorizeRequest

Action Filters

• Action Filters are attributes applied to actions

• Can execute code before/after action, independently of the action

– Authorization

– Logging

– Exception handling

– Output caching

• Build your own based on ActionFilterAttribute

Page 20: ASP.NET WebForms vs. ASP.NET MVC - Michiel van Otegemmichiel.vanotegem.nl/content/binary/WebForms vs MVC.pdf · Request Response BeginRequest AuthenticateRequest AuthorizeRequest

Testing

ASP.NET MVC Apps

• MVC removes UI dependency easier to test

• Interfaces and abstract base classes to mock/stub

– IViewEngine

– HttpSessionStateBase

– HttpContextBase

• Works with different testing frameworks

– NUnit, XUnit, MBUnit

Page 21: ASP.NET WebForms vs. ASP.NET MVC - Michiel van Otegemmichiel.vanotegem.nl/content/binary/WebForms vs MVC.pdf · Request Response BeginRequest AuthenticateRequest AuthorizeRequest

WebForms vs. MVC

WebForms

• Intuitive model, mimics real life

• Abstracts HTTP protocol

• Page/Controls render output

• Focused on one UI type: HTML

• Hard to test UI

• Many existing apps and controls

• Good RAD platform

MVC

• Indirection less intuitive

• Direct interaction with HTTP

• View enables full output control

• Usable for many UIs

• UI testable by testing controller

• Relatively new, less available

• Helpers make life easier

Page 22: ASP.NET WebForms vs. ASP.NET MVC - Michiel van Otegemmichiel.vanotegem.nl/content/binary/WebForms vs MVC.pdf · Request Response BeginRequest AuthenticateRequest AuthorizeRequest

Summary

• MVC is a new model in ASP.NET

• Does not replace WebForms Choice

• Benefits from pre-existing WebForms features

• Optimized for web: Clean HTML and URLs

• Separation of concerns enables cleaner code

– Indirection must be understood by developers

– Allows for testability and extensibility

Page 23: ASP.NET WebForms vs. ASP.NET MVC - Michiel van Otegemmichiel.vanotegem.nl/content/binary/WebForms vs MVC.pdf · Request Response BeginRequest AuthenticateRequest AuthorizeRequest

Michiel van Otegem

mail: [email protected]

www: michiel.vanotegem.nl