Introduction to ASP.NET MVC
Learn More @ http://www.learnnowonline.com Copyright Š by Application Developers Training Company
Objectives • Understand the basics of the Model-ViewController pattern • Explore the differences between ASP.NET Web forms and MVC applications • Build a sample MVC application and explore its structure
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
Agenda • The Model-View-Controller Pattern • Differences Between MVC and Web Forms Applications • Building a Simple MVC Application with Visual Studio
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
The Model-View-Controller Pattern • ASP.NET MVC is a revolutionary way to develop Web applications
Built on Model-View-Controller pattern Separation of concerns Invented in 1978 by Trygve Reenskaug Very popular in Web development frameworks
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
The MVC Architecture • Three primary components Model View Controller
• User interaction with application 1.Interacts with Web page 2.Controller handles request 3.Notifies model of changes Controller selects a view Await new user interaction Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
Anatomy of a Request HTTP Request HTML Response
View
Controller
Model
Optional Persistent Storage
Learn More @ http://www.learnnowonline.com Copyright Š by Application Developers Training Company
ASP.NET MVC • Microsoft’s newest Web development framework Easy to implement design principles and patterns Integral part of ASP.NET
• First implementation by Scott Guthrie • Built using core ASP.NET features
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
Differences Between MVC and Web Forms Applications • ASP.NET Web forms was revolutionary in its day
Rich infrastructure for Web version of desktop applications Abstracts limitations of HTTP Uses ViewState as a major feature Drag and drop development interface
• Problems with Web forms
Little control over HTML ViewState can bloat Limited support for testing Complicated page lifecycle
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
The Tenets of MVC • • • •
Separation of concerns Convention over configuration Keep it DRY: Don’t Repeat Yourself Be helpful, but get out of my way
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
Web Forms vs. MVC Extensibility • ASP.NET has a provider model • MVC has a more powerful pluggable model • Options Use the default component Extend the default component Replace the default component
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
Web Forms vs. MVC Blending of Concerns
• Web forms almost forces combination of view and controller • False sense of separation from code behind files Reality: close coupling
• MVC’s separation of concerns encourages good coding practices Ideally suited to Web application development
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
Web Forms vs. MVC
MVC Serves Methods, Not Files • Huge difference • File request: http://www.example.com/index.aspx?ID=5 • Method request: http://www.example.com/Home/Details/5 Maps to Details action method Of Home controller Item ID of 5
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
Web Forms vs. MVC Testability
• Testing helps make applications more robust in the face of changes • Unit test automates verification of a method • Can use the test-driven development methodology • Web forms make automated testing almost impossible
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
Web Forms vs. MVC Control over HTML
• Web forms server controls provide rich functionality Produce unreadable, monolithic blocks of HTML
• MVC gives you complete control over HTML
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
Web Forms vs. MVC Open Source
• MVC source code is available • Liberal open source license: Microsoft Public License
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
Building a Simple MVC Application with Visual Studio • Building MVC applications can be efficient, because: Web forms often require fragmented code MVC’s convention over configuration MVC tools in Visual Studio 2008 and 2010
• Best of all worlds with MVC
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
MVC Project Templates • Includes one Visual Studio Project template Available for VB and C#
• Includes templates for three project types Internet Application template o
Basic but fully functional MVC application
Intranet Application template o
Same as Internet template, but uses Windows authentication
Empty template o
Sets up project structure but no controllers or models
• Use whichever template you like Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
Upgrading from MVC 2 to 3 • Can install MVC 3 side-by-side with MVC 1 and 2 • When ready, upgrade to MVC 3 • Process is manual Change a GUID, copy files, change web.config, etc. Better to just create a new project and move stuff? Release notes cover the basic steps
• Microsoft’s Eilon Lipton has an unsupported migration tool
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
Learn More! • This is an excerpt from a larger course. Visit www.learnnowonline.com for the full details! • Learn more about MVC on SlideShare: • Working with Controllers and Actions in MVC
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company