msdn - asp.net mvc
DESCRIPTION
Introduction to the ASP.NET MVC framework. (Belgium MSDN session)TRANSCRIPT
![Page 1: MSDN - ASP.NET MVC](https://reader031.vdocuments.net/reader031/viewer/2022012321/554f4af7b4c905524c8b4955/html5/thumbnails/1.jpg)
ASP.NET MVC(model-view-controller)
Maarten Balliauw – RealDolmenhttp://blog.maartenballiauw.be
![Page 2: MSDN - ASP.NET MVC](https://reader031.vdocuments.net/reader031/viewer/2022012321/554f4af7b4c905524c8b4955/html5/thumbnails/2.jpg)
Who am I?
• Maarten Balliauw• Antwerp, Belgium• www.realdolmen.com• Focus on web
– ASP.NET, ASP.NET MVC, PHP, Azure, VSTS, …
• http://blog.maartenballiauw.be• http://twitter.com/maartenballiauw
![Page 3: MSDN - ASP.NET MVC](https://reader031.vdocuments.net/reader031/viewer/2022012321/554f4af7b4c905524c8b4955/html5/thumbnails/3.jpg)
Agenda
• Overview of ASP.NET• ASP.NET MVC• Building an application• Unit testing• Should I choose Webforms or MVC?• Q&A
![Page 4: MSDN - ASP.NET MVC](https://reader031.vdocuments.net/reader031/viewer/2022012321/554f4af7b4c905524c8b4955/html5/thumbnails/4.jpg)
ASP.NET…
• “one web application framework to rule them all”
• Always seen as one whole…
CachingCaching ModulesModules
HandlersHandlersIntrinsicsIntrinsics
PagesPages ControlsControls
GlobalizationGlobalization
ProfileProfile
Master PagesMaster Pages
MembershipMembershipRolesRoles
Etc.Etc.
ASP.NET
![Page 5: MSDN - ASP.NET MVC](https://reader031.vdocuments.net/reader031/viewer/2022012321/554f4af7b4c905524c8b4955/html5/thumbnails/5.jpg)
Look at it differently!
• Core infrastructure• Different presentation options
ASP.NETDynamic Data
ASP.NETWebForms
ASP.NETMVC
Presentation
RuntimeASP.NET
Core
![Page 6: MSDN - ASP.NET MVC](https://reader031.vdocuments.net/reader031/viewer/2022012321/554f4af7b4c905524c8b4955/html5/thumbnails/6.jpg)
ASP.NET MVC
• A new presentation option for ASP.NET• Simpler way to program ASP.NET• Easily testable and TDD friendly• More control over your <html/>• More control over your URLs• Not for everyone! (car vs. motorcycle)• Not a replacement for webforms!• Supports existing ASP.NET features
![Page 7: MSDN - ASP.NET MVC](https://reader031.vdocuments.net/reader031/viewer/2022012321/554f4af7b4c905524c8b4955/html5/thumbnails/7.jpg)
Things you will lose…
• Viewstate• Postbacks• Tricky interview questions about nuances of the
page lifecycle
![Page 8: MSDN - ASP.NET MVC](https://reader031.vdocuments.net/reader031/viewer/2022012321/554f4af7b4c905524c8b4955/html5/thumbnails/8.jpg)
MVC? Model-View-Controller!
ControllerController(Input)(Input)
ModelModel(Logic)(Logic)
ViewView(Presentation)(Presentation)
Separation of concerns!
![Page 9: MSDN - ASP.NET MVC](https://reader031.vdocuments.net/reader031/viewer/2022012321/554f4af7b4c905524c8b4955/html5/thumbnails/9.jpg)
What does an MVC request look like?
Request
View
Controller
Response
ControllerHandles input(HTTP requests)
ViewVisually representsthe model
ViewData
![Page 10: MSDN - ASP.NET MVC](https://reader031.vdocuments.net/reader031/viewer/2022012321/554f4af7b4c905524c8b4955/html5/thumbnails/10.jpg)
DEMOFile > New…
![Page 11: MSDN - ASP.NET MVC](https://reader031.vdocuments.net/reader031/viewer/2022012321/554f4af7b4c905524c8b4955/html5/thumbnails/11.jpg)
Framework Goals
• Easy and frictionless testability• Full control over your <html/>• Full control over your URLs• Leverage existing ASP.NET features• Conventions and guidance• …
![Page 12: MSDN - ASP.NET MVC](https://reader031.vdocuments.net/reader031/viewer/2022012321/554f4af7b4c905524c8b4955/html5/thumbnails/12.jpg)
Clean URLs
Would you use:
/Products.aspx?
CategoryID={3F2504E0-4F89-11D3-
9A0C-0305E82C3301}
Or:
/Products/Books
My Favorite
![Page 13: MSDN - ASP.NET MVC](https://reader031.vdocuments.net/reader031/viewer/2022012321/554f4af7b4c905524c8b4955/html5/thumbnails/13.jpg)
Extensibility
• These come into play…
• … and are all replacable!
ControllerBuilder
ControllerFactory
Controller
ViewEngine
View
ControllerActionInvoker
ActionResult
ActionFilters
Model Binders
![Page 14: MSDN - ASP.NET MVC](https://reader031.vdocuments.net/reader031/viewer/2022012321/554f4af7b4c905524c8b4955/html5/thumbnails/14.jpg)
DEMOBuilding an ASP.NET MVC application
![Page 15: MSDN - ASP.NET MVC](https://reader031.vdocuments.net/reader031/viewer/2022012321/554f4af7b4c905524c8b4955/html5/thumbnails/15.jpg)
Testing
• Has anyone tried testing webforms?• Without IIS being fired up?• Each component tested individually?• Did you require vacation afterwards?
![Page 16: MSDN - ASP.NET MVC](https://reader031.vdocuments.net/reader031/viewer/2022012321/554f4af7b4c905524c8b4955/html5/thumbnails/16.jpg)
Infrastructure designed for testing
• These are all easily mockable!– HttpContextBase, HttpResponseBase, HttpRequestBase
• Extensibility – IController– IControllerFactory– IRouteHandler– IViewEngine, IView
![Page 17: MSDN - ASP.NET MVC](https://reader031.vdocuments.net/reader031/viewer/2022012321/554f4af7b4c905524c8b4955/html5/thumbnails/17.jpg)
Testing controller actions
• No requirement to test within ASP.NET runtime!– Can mock parts of runtime you want to fake– Using Moq, Rhino, TypeMock, …
• http://code.google.com/p/moq/
[TestMethod]public void ShowPostsDisplayPostView() { BlogController controller = new BlogController(…); var result = controller.ShowPost(2) as ViewResult;
Assert.IsNotNull(result); Assert.AreEqual(result.ViewData["Message"], "Hello");}
[TestMethod]public void ShowPostsDisplayPostView() { BlogController controller = new BlogController(…); var result = controller.ShowPost(2) as ViewResult;
Assert.IsNotNull(result); Assert.AreEqual(result.ViewData["Message"], "Hello");}
![Page 18: MSDN - ASP.NET MVC](https://reader031.vdocuments.net/reader031/viewer/2022012321/554f4af7b4c905524c8b4955/html5/thumbnails/18.jpg)
Testing controller actions using Moq
More on Mocking?Visit VISUG on May 7, 2009 – www.visug.be)
[TestMethod]public void TestInvalidCredentials(){ LoginController controller = new LoginController();
var mock = new Mock<System.Web.Security.MembershipProvider>(); mock.Expect(m => m.ValidateUser("", "")).Returns(false); controller.MembershipProviderInstance = mock.Object;
var result = controller.Authenticate("", "") as ViewResult;
Assert.IsNotNull(result); Assert.AreEqual(result.ViewName, "Index"); Assert.AreEqual(controller.ViewData["ErrorMessage"], "Invalid credentials! Please verify your username and password.");}
[TestMethod]public void TestInvalidCredentials(){ LoginController controller = new LoginController();
var mock = new Mock<System.Web.Security.MembershipProvider>(); mock.Expect(m => m.ValidateUser("", "")).Returns(false); controller.MembershipProviderInstance = mock.Object;
var result = controller.Authenticate("", "") as ViewResult;
Assert.IsNotNull(result); Assert.AreEqual(result.ViewName, "Index"); Assert.AreEqual(controller.ViewData["ErrorMessage"], "Invalid credentials! Please verify your username and password.");}
![Page 19: MSDN - ASP.NET MVC](https://reader031.vdocuments.net/reader031/viewer/2022012321/554f4af7b4c905524c8b4955/html5/thumbnails/19.jpg)
Testing frameworks
• Any framework is supported!– Visual Studio Test– NUnit– XUnit– MBUnit– …
![Page 20: MSDN - ASP.NET MVC](https://reader031.vdocuments.net/reader031/viewer/2022012321/554f4af7b4c905524c8b4955/html5/thumbnails/20.jpg)
DEMOUnit testing
![Page 21: MSDN - ASP.NET MVC](https://reader031.vdocuments.net/reader031/viewer/2022012321/554f4af7b4c905524c8b4955/html5/thumbnails/21.jpg)
Choosing between Webforms and MVC
ASP.NET Webforms• Winforms-alike event
model• Familiar controls• Familiar = rapid application
development• Functionality per page• Uses viewstate for state
management• Less control over rendered
HTML
ASP.NET MVC• Less complex: separation
of concerns• Easier parallel
development• TDD support• No viewstate, …• Full control over behavior
and HTML• Makes you think
![Page 22: MSDN - ASP.NET MVC](https://reader031.vdocuments.net/reader031/viewer/2022012321/554f4af7b4c905524c8b4955/html5/thumbnails/22.jpg)
So what do I choose?
• Go with the flow!
• Remember:mixing of worlds is possible!– Mixing with ASP.NET Webforms– Mixing with dynamic data– …
![Page 23: MSDN - ASP.NET MVC](https://reader031.vdocuments.net/reader031/viewer/2022012321/554f4af7b4c905524c8b4955/html5/thumbnails/23.jpg)
Takeaways
• A new option for ASP.NET– Not a replacement for Webforms!
• More control over your <html/> and URLs
• Strong, frictionless testability
![Page 24: MSDN - ASP.NET MVC](https://reader031.vdocuments.net/reader031/viewer/2022012321/554f4af7b4c905524c8b4955/html5/thumbnails/24.jpg)
Resources• “ASP.NET MVC 1.0 Quickly”
http://tinyurl.com/mvcquickly
• My bloghttp://blog.maartenballiauw.be/category/MVC.aspx
• Microsofthttp://www.asp.net/mvchttp://wiki.asp.net/page.aspx/286/aspnet-mvc-framework/
• http://blog.wekeroad.com/blog/i-spose-ill-just-say-it-you-should-learn-mvc/
• Community projectshttp://mvccontrib.codeplex.comhttp://mvcsitemap.codeplex.com
![Page 26: MSDN - ASP.NET MVC](https://reader031.vdocuments.net/reader031/viewer/2022012321/554f4af7b4c905524c8b4955/html5/thumbnails/26.jpg)
© 2008 Microsoft Corporation. All rights reserved. Microsoft, Windows, 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.