![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](https://reader034.vdocuments.us/reader034/viewer/2022042512/5f61f38078283527f441c5df/html5/thumbnails/1.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042512/5f61f38078283527f441c5df/html5/thumbnails/2.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042512/5f61f38078283527f441c5df/html5/thumbnails/3.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042512/5f61f38078283527f441c5df/html5/thumbnails/4.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042512/5f61f38078283527f441c5df/html5/thumbnails/5.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042512/5f61f38078283527f441c5df/html5/thumbnails/6.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042512/5f61f38078283527f441c5df/html5/thumbnails/7.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042512/5f61f38078283527f441c5df/html5/thumbnails/8.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042512/5f61f38078283527f441c5df/html5/thumbnails/9.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042512/5f61f38078283527f441c5df/html5/thumbnails/10.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042512/5f61f38078283527f441c5df/html5/thumbnails/11.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042512/5f61f38078283527f441c5df/html5/thumbnails/12.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042512/5f61f38078283527f441c5df/html5/thumbnails/13.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042512/5f61f38078283527f441c5df/html5/thumbnails/14.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042512/5f61f38078283527f441c5df/html5/thumbnails/15.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042512/5f61f38078283527f441c5df/html5/thumbnails/16.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042512/5f61f38078283527f441c5df/html5/thumbnails/17.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042512/5f61f38078283527f441c5df/html5/thumbnails/18.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042512/5f61f38078283527f441c5df/html5/thumbnails/19.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042512/5f61f38078283527f441c5df/html5/thumbnails/20.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042512/5f61f38078283527f441c5df/html5/thumbnails/21.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042512/5f61f38078283527f441c5df/html5/thumbnails/22.jpg)
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