Introducing the ASP.NET MVC framework Maarten  Balliauw –  Real Dolmen Website:  www.realdolmen.com   E-mail:  [email_address]   Blog:  http://blog.maartenballiauw.be
AGENDA MVC ASP.NET MVC Hello world! Routing Extensibility Testing …
MVC – MODEL-VIEW-CONTROLLER Design pattern Isolating business logic from UI Maintainability Layering for UI layer
ASP.NET MVC A new  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
WHAT YOU WILL LOSE… Viewstate Postbacks Tricky interview questions about nuances of the page lifecycle
HOW IT WORKS… Browser Web Server http://myserver.com/Products/Edit/5 http://myserver.com/Products/
INTERACTIONS /Products/Edit/5 ProductsController (Controller) /Products/Update/5 URL:
DEMO Hello World
WHAT’S THE POINT? It’s not WebForms 4.0! It’s an  alternative Flexible Extendible in each part of the process Fundamental Only System.Web!  (Not WPF or Winforms) Plays well with others NHibernate for models, NVelocity for views, Unity for dependency injection, … Keep things simple!
GOALS Maintain Clean Separation of Concerns Easy Testing  Red/Green TDD  Highly maintainable applications by default Extensible and Pluggable Support replacing any component of system Source available on CodePlex Not open source Build your own, private version
DRIVING GOALS Enable clean URLs and SEO SEO friendly URL structures Great ASP.NET integration All providers are supported Membership, Caching, Session, … ASP.NET designer in VS2008 Check my blog! http://tinyurl.com/maarten-outputcache http://tinyurl.com/maarten-mvcsitemap
REQUEST LIFECYCLE Browser requests /Products/ Route is determined Controller is activated Method on Controller is invoked Controller does some stuff Renders View, passing in ViewData
ACTUALLY, THERE’S MORE… You can replace each step of the process!
DEMO Request lifecycle
ROUTING – PRETTY URLS Developer adds Routes to a global RouteTable Mapping creates RouteData – a bag of key/values This is  NOT  URL rewriting! It’s more! public static void RegisterRoutes(RouteCollection routes) {   routes.MapRoute( &quot;Default &quot;{controller}/{action}/{id}&quot;,  new { controller = &quot;Home&quot;, action = &quot;Index&quot;, id = &quot;&quot; }, new { controller = @&quot;[^\.]*&quot; } ); }
CUSTOM URL ROUTING Can setup custom URL routing rules within the RegisterRoutes() method in Global.asax Enables very flexible mapping and route rules: Default parameters Constraints Named routes Wildcard “catch-all” routes
DEMO Routing
EXTENSIBILITY These are all pluggable! Views Controllers Models Routes Check my blog! http://tinyurl.com/maarten-mvccustomviewengine MvcContrib http://www.codeplex.com/MvcContrib
VIEWENGINE, VIEW View engines instantiate views Views render output You get WebForms by default Can implement your own MvcContrib has them for Brail, NHaml, NVelocity, … Customizing these can be used to Offer new DSLs to make HTML easier Generate totally different mime/types Images, RSS, JSON, XML, OFX, VCards, DOCX, whatever public abstract class VirtualPathProviderViewEngine : IViewEngine { protected abstract IView CreatePartialView(...); protected abstract IView CreateView(…); } public interface IView { void Render(ViewContext viewContext, TextWriter writer); }
NHAML – FROM… <%@ Page Language=&quot;C#&quot; MasterPageFile=&quot;~/Views/Shared/Site.Master&quot; AutoEventWireup=&quot;true&quot;  CodeBehind=&quot;List.aspx&quot; Inherits=&quot;MvcApplication5.Views.Products.List&quot; Title=&quot;Products&quot; %> <asp:Content ContentPlaceHolderID=&quot;MainContentPlaceHolder&quot; runat=&quot;server&quot;> <h2><%= ViewData.CategoryName %></h2> <ul> <% foreach (var product in ViewData.Products) { %> <li> <%= product.ProductName %>  <div class=&quot;editlink&quot;> (<%= Html.ActionLink(&quot;Edit&quot;, new { Action=&quot;Edit&quot;, ID=product.ProductID })%>) </div> </li> <% } %> </ul> <%= Html.ActionLink(&quot;Add New Product&quot;, new { Action=&quot;New&quot; }) %> </asp:Content>
NHAML – TO… http://code.google.com/p/nhaml/ %h2= ViewData.CategoryName  %ul  - foreach (var product in ViewData.Products)   %li = product.ProductName    .editlink   = Html.ActionLink(&quot;Edit&quot;,    new { Action=&quot;Edit&quot;,    ID=product.ProductID })    = Html.ActionLink(&quot;Add New Product&quot;,    new { Action=&quot;New&quot; })
SPARK – FROM… <%@ Page Language=&quot;C#&quot; MasterPageFile=&quot;~/Views/Shared/Site.Master&quot; AutoEventWireup=&quot;true&quot;  CodeBehind=&quot;List.aspx&quot; Inherits=&quot;MvcApplication5.Views.Products.List&quot; Title=&quot;Products&quot; %> <asp:Content ContentPlaceHolderID=&quot;MainContentPlaceHolder&quot; runat=&quot;server&quot;> <h2><%= ViewData.CategoryName %></h2> <ul> <% foreach (var product in ViewData.Products) { %> <li> <%= product.ProductName %>  <div class=&quot;editlink&quot;> (<%= Html.ActionLink(&quot;Edit&quot;, new { Action=&quot;Edit&quot;, ID=product.ProductID })%>) </div> </li> <% } %> </ul> <%= Html.ActionLink(&quot;Add New Product&quot;, new { Action=&quot;New&quot; }) %> </asp:Content>
SPARK – TO… http://dev.dejardin.org/ <viewdata model=&quot;MvcApplication5.Models.Product“/> <h2>${ViewData.CategoryName}</h2> <ul>   <for each=&quot;var product in ViewData.Products&quot;>   <li> ${product.ProductName}   <div class=&quot;editlink&quot;>   (<a href=&quot;/Products/Edit/${product.ProductID}&quot;>Edit</a>)   </div> </li> </for> </ul> <a href=&quot;/Products/New&quot;>Add New Product</a>
FILTER ATTRIBUTES Enable custom behavior to be added to Controllers and Controller actions 4 types Authentication filter Action filter Result filter Exception filter Examples: [Authorize], [LogAction], [OutputCache], … http://tinyurl.com/maarten-outputcache
DEMO Filter attributes
TESTING Has anyone tried testing webforms? Without IIS being fired up? Each component tested individually?
INTERFACES AND TESTING These are all easily mockable! HttpContextBase, HttpResponseBase, HttpRequestBase Extensibility  IController IControllerFactory IRouteHandler IViewEngine
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[ &quot; Message &quot; ],  &quot; Hello &quot; ); }
TESTING CONTROLLER ACTIONS USING MOQ [TestMethod] public void TestInvalidCredentials() { LoginController controller = new LoginController(); var mock = new Mock<System.Web.Security.MembershipProvider>(); mock.Expect(m => m.ValidateUser(&quot;&quot;, &quot;&quot;)).Returns(false); controller.MembershipProviderInstance = mock.Object; var result = controller.Authenticate(&quot;&quot;, &quot;&quot;) as ViewResult; Assert.IsNotNull(result); Assert.AreEqual(result.ViewName, &quot;Index&quot;); Assert.AreEqual(controller.ViewData[&quot;ErrorMessage&quot;], &quot;Invalid credentials! Please verify your username and password.&quot;); }
TESTING FRAMEWORKS Any framework is supported! Visual Studio Test NUnit XUnit …
DEMO Testing
DEMO A complete application!
SUMMARY A new option for ASP.NET. More control over your <html/> and URLs More easily testable approach Not for everyone – only use it if you want to Shipping later this year
COMMON QUESTIONS Should I use WebForms or MVC? Are there any controls? Do I have to write <%= %> code in my view? What about AJAX? How fast/scalable is it? Is it safe? When will it ship?
QUESTIONS?
THANK YOU! Make sure to check http://blog.maartenballiauw.be/category/MVC.aspx

More Related Content

PPT
How do speed up web pages? CSS & HTML Tricks
PDF
Top 7 Angular Best Practices to Organize Your Angular App
ODP
Devoxx 09 (Belgium)
PPTX
Behat - Drupal South 2018
PDF
How to Implement Basic Angular Routing and Nested Routing With Params in Angu...
PDF
Testing Web Applications
PDF
Night Watch with QA
PDF
ForwardJS 2017 - Fullstack end-to-end Test Automation with node.js
How do speed up web pages? CSS & HTML Tricks
Top 7 Angular Best Practices to Organize Your Angular App
Devoxx 09 (Belgium)
Behat - Drupal South 2018
How to Implement Basic Angular Routing and Nested Routing With Params in Angu...
Testing Web Applications
Night Watch with QA
ForwardJS 2017 - Fullstack end-to-end Test Automation with node.js

What's hot (20)

PDF
Usability in the GeoWeb
PPTX
Introduction to angular js for .net developers
PPTX
Testing C# and ASP.net using Ruby
PPTX
Angularjs Live Project
PDF
Web driver selenium simplified
PDF
Fullstack End-to-end test automation with Node.js, one year later
PDF
Laravel mail example how to send an email using markdown template in laravel 8
PDF
Introduction to Using PHP & MVC Frameworks
PDF
JavaFX Advanced
PPT
Rey Bango - HTML5: polyfills and shims
PDF
Node.js and Selenium Webdriver, a journey from the Java side
PPTX
Migrating from JSF1 to JSF2
PDF
JavaFX in Action (devoxx'16)
PDF
API Technical Writing
PDF
OSDC 2009 Rails Turtorial
PPTX
Add even richer interaction to your site - plone.patternslib
PDF
Beginning AngularJS
PPT
Perlbal Tutorial
PDF
A tech writer, a map, and an app
PPT
What's new and exciting with JSF 2.0
Usability in the GeoWeb
Introduction to angular js for .net developers
Testing C# and ASP.net using Ruby
Angularjs Live Project
Web driver selenium simplified
Fullstack End-to-end test automation with Node.js, one year later
Laravel mail example how to send an email using markdown template in laravel 8
Introduction to Using PHP & MVC Frameworks
JavaFX Advanced
Rey Bango - HTML5: polyfills and shims
Node.js and Selenium Webdriver, a journey from the Java side
Migrating from JSF1 to JSF2
JavaFX in Action (devoxx'16)
API Technical Writing
OSDC 2009 Rails Turtorial
Add even richer interaction to your site - plone.patternslib
Beginning AngularJS
Perlbal Tutorial
A tech writer, a map, and an app
What's new and exciting with JSF 2.0
Ad

Viewers also liked (20)

PPTX
ASP.NET MVC Wisdom
PPTX
Just another Wordpress weblog, but more cloudy
PPTX
Mocking - Visug session
PPTX
AZUG.BE - Azure User Group Belgium - First public meeting
PPTX
PHP And Silverlight - DevDays session
PPTX
PPTX
MSDN - Converting an existing ASP.NET application to Windows Azure
PPTX
Presentation Thesis Big Data
PPT
MSDN - ASP.NET MVC
PDF
Bài 6 an toàn hệ thống máy tính
PDF
Bài 3: Lập trình giao diện điều khiển & Xử lý sự kiện - Lập trình winform - G...
PDF
Bài 5: Làm quen với lập trình CSDL ASP.NET - Giáo trình FPT - Có ví dụ kèm theo
PDF
Bài 7: Lập trình với CSDL – Sử dụng DESIGNER & Triển khai ứng dụng - Lập trìn...
PDF
Bài 4: Lập trình với CSDL ADO.NET & Kiến trúc không kết nối & Lập trình giao ...
PDF
Bài 6: Điều khiển DetailsView, FormView, ListView, DataPager
PDF
Bài 1 - Làm quen với C# - Lập trình winform
PDF
BÀI 2: Thiết kế FORM và xử lý sự kiện - Giáo trình FPT
PDF
Apache Hadoop YARN - Enabling Next Generation Data Applications
PDF
Lập trình ứng dụng web asp.net với C# - tailieumienphi.edu.vn
PDF
Lập trình sáng tạo creative computing textbook mastercode.vn
ASP.NET MVC Wisdom
Just another Wordpress weblog, but more cloudy
Mocking - Visug session
AZUG.BE - Azure User Group Belgium - First public meeting
PHP And Silverlight - DevDays session
MSDN - Converting an existing ASP.NET application to Windows Azure
Presentation Thesis Big Data
MSDN - ASP.NET MVC
Bài 6 an toàn hệ thống máy tính
Bài 3: Lập trình giao diện điều khiển & Xử lý sự kiện - Lập trình winform - G...
Bài 5: Làm quen với lập trình CSDL ASP.NET - Giáo trình FPT - Có ví dụ kèm theo
Bài 7: Lập trình với CSDL – Sử dụng DESIGNER & Triển khai ứng dụng - Lập trìn...
Bài 4: Lập trình với CSDL ADO.NET & Kiến trúc không kết nối & Lập trình giao ...
Bài 6: Điều khiển DetailsView, FormView, ListView, DataPager
Bài 1 - Làm quen với C# - Lập trình winform
BÀI 2: Thiết kế FORM và xử lý sự kiện - Giáo trình FPT
Apache Hadoop YARN - Enabling Next Generation Data Applications
Lập trình ứng dụng web asp.net với C# - tailieumienphi.edu.vn
Lập trình sáng tạo creative computing textbook mastercode.vn
Ad

Similar to Introduction to ASP.NET MVC (20)

PPT
CTTDNUG ASP.NET MVC
PPT
ASP.NET MVC introduction
PPS
Introduction To Mvc
PPT
Spring MVC
PPTX
ASP.NET MVC
PPT
Introduction To ASP.NET MVC
ODP
WebTest - Efficient Functional Web Testing with HtmlUnit and Beyond
PPTX
Asp.Net MVC Intro
PPT
EPiServer Web Parts
PPT
Introduction to ASP.NET MVC
PPT
ASP.NET MVC Presentation
PPTX
A Web Developer's Journey across different versions of ASP.NET
PPTX
Asp.Net Mvc
PPT
You Know WebOS
PDF
Asp.Net MVC Framework Design Pattern
PPTX
Developing ASP.NET Applications Using the Model View Controller Pattern
PPTX
MVC & SQL_In_1_Hour
PPTX
Overview of MVC Framework - by software outsourcing company india
PPTX
Usability AJAX and other ASP.NET Features
PPT
ASP.net MVC CodeCamp Presentation
CTTDNUG ASP.NET MVC
ASP.NET MVC introduction
Introduction To Mvc
Spring MVC
ASP.NET MVC
Introduction To ASP.NET MVC
WebTest - Efficient Functional Web Testing with HtmlUnit and Beyond
Asp.Net MVC Intro
EPiServer Web Parts
Introduction to ASP.NET MVC
ASP.NET MVC Presentation
A Web Developer's Journey across different versions of ASP.NET
Asp.Net Mvc
You Know WebOS
Asp.Net MVC Framework Design Pattern
Developing ASP.NET Applications Using the Model View Controller Pattern
MVC & SQL_In_1_Hour
Overview of MVC Framework - by software outsourcing company india
Usability AJAX and other ASP.NET Features
ASP.net MVC CodeCamp Presentation

More from Maarten Balliauw (20)

PPTX
Bringing nullability into existing code - dammit is not the answer.pptx
PPTX
Nerd sniping myself into a rabbit hole... Streaming online audio to a Sonos s...
PPTX
Building a friendly .NET SDK to connect to Space
PPTX
Microservices for building an IDE - The innards of JetBrains Rider - NDC Oslo...
PPTX
Indexing and searching NuGet.org with Azure Functions and Search - .NET fwday...
PPTX
NDC Sydney 2019 - Microservices for building an IDE – The innards of JetBrain...
PPTX
JetBrains Australia 2019 - Exploring .NET’s memory management – a trip down m...
PPTX
.NET Conf 2019 - Indexing and searching NuGet.org with Azure Functions and Se...
PPTX
CloudBurst 2019 - Indexing and searching NuGet.org with Azure Functions and S...
PPTX
NDC Oslo 2019 - Indexing and searching NuGet.org with Azure Functions and Search
PPTX
Approaches for application request throttling - Cloud Developer Days Poland
PPTX
Indexing and searching NuGet.org with Azure Functions and Search - Cloud Deve...
PPTX
Approaches for application request throttling - dotNetCologne
PPTX
CodeStock - Exploring .NET memory management - a trip down memory lane
PPTX
ConFoo Montreal - Microservices for building an IDE - The innards of JetBrain...
PPTX
ConFoo Montreal - Approaches for application request throttling
PPTX
Microservices for building an IDE – The innards of JetBrains Rider - TechDays...
PPTX
JetBrains Day Seoul - Exploring .NET’s memory management – a trip down memory...
PPTX
DotNetFest - Let’s refresh our memory! Memory management in .NET
PPTX
VISUG - Approaches for application request throttling
Bringing nullability into existing code - dammit is not the answer.pptx
Nerd sniping myself into a rabbit hole... Streaming online audio to a Sonos s...
Building a friendly .NET SDK to connect to Space
Microservices for building an IDE - The innards of JetBrains Rider - NDC Oslo...
Indexing and searching NuGet.org with Azure Functions and Search - .NET fwday...
NDC Sydney 2019 - Microservices for building an IDE – The innards of JetBrain...
JetBrains Australia 2019 - Exploring .NET’s memory management – a trip down m...
.NET Conf 2019 - Indexing and searching NuGet.org with Azure Functions and Se...
CloudBurst 2019 - Indexing and searching NuGet.org with Azure Functions and S...
NDC Oslo 2019 - Indexing and searching NuGet.org with Azure Functions and Search
Approaches for application request throttling - Cloud Developer Days Poland
Indexing and searching NuGet.org with Azure Functions and Search - Cloud Deve...
Approaches for application request throttling - dotNetCologne
CodeStock - Exploring .NET memory management - a trip down memory lane
ConFoo Montreal - Microservices for building an IDE - The innards of JetBrain...
ConFoo Montreal - Approaches for application request throttling
Microservices for building an IDE – The innards of JetBrains Rider - TechDays...
JetBrains Day Seoul - Exploring .NET’s memory management – a trip down memory...
DotNetFest - Let’s refresh our memory! Memory management in .NET
VISUG - Approaches for application request throttling

Recently uploaded (20)

PPTX
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
PPTX
TEXTILE technology diploma scope and career opportunities
PDF
OpenACC and Open Hackathons Monthly Highlights July 2025
DOCX
search engine optimization ppt fir known well about this
PDF
Developing a website for English-speaking practice to English as a foreign la...
PPT
Galois Field Theory of Risk: A Perspective, Protocol, and Mathematical Backgr...
PDF
Credit Without Borders: AI and Financial Inclusion in Bangladesh
PDF
Consumable AI The What, Why & How for Small Teams.pdf
PDF
Statistics on Ai - sourced from AIPRM.pdf
PPT
What is a Computer? Input Devices /output devices
PDF
UiPath Agentic Automation session 1: RPA to Agents
PDF
sbt 2.0: go big (Scala Days 2025 edition)
PPTX
Microsoft Excel 365/2024 Beginner's training
PDF
Flame analysis and combustion estimation using large language and vision assi...
PDF
Zenith AI: Advanced Artificial Intelligence
PDF
STKI Israel Market Study 2025 version august
PDF
A proposed approach for plagiarism detection in Myanmar Unicode text
PDF
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
PDF
Architecture types and enterprise applications.pdf
PDF
Convolutional neural network based encoder-decoder for efficient real-time ob...
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
TEXTILE technology diploma scope and career opportunities
OpenACC and Open Hackathons Monthly Highlights July 2025
search engine optimization ppt fir known well about this
Developing a website for English-speaking practice to English as a foreign la...
Galois Field Theory of Risk: A Perspective, Protocol, and Mathematical Backgr...
Credit Without Borders: AI and Financial Inclusion in Bangladesh
Consumable AI The What, Why & How for Small Teams.pdf
Statistics on Ai - sourced from AIPRM.pdf
What is a Computer? Input Devices /output devices
UiPath Agentic Automation session 1: RPA to Agents
sbt 2.0: go big (Scala Days 2025 edition)
Microsoft Excel 365/2024 Beginner's training
Flame analysis and combustion estimation using large language and vision assi...
Zenith AI: Advanced Artificial Intelligence
STKI Israel Market Study 2025 version august
A proposed approach for plagiarism detection in Myanmar Unicode text
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
Architecture types and enterprise applications.pdf
Convolutional neural network based encoder-decoder for efficient real-time ob...

Introduction to ASP.NET MVC

  • 1.  
  • 2. Introducing the ASP.NET MVC framework Maarten Balliauw – Real Dolmen Website: www.realdolmen.com E-mail: [email_address] Blog: http://blog.maartenballiauw.be
  • 3. AGENDA MVC ASP.NET MVC Hello world! Routing Extensibility Testing …
  • 4. MVC – MODEL-VIEW-CONTROLLER Design pattern Isolating business logic from UI Maintainability Layering for UI layer
  • 5. ASP.NET MVC A new 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
  • 6. WHAT YOU WILL LOSE… Viewstate Postbacks Tricky interview questions about nuances of the page lifecycle
  • 7. HOW IT WORKS… Browser Web Server http://myserver.com/Products/Edit/5 http://myserver.com/Products/
  • 8. INTERACTIONS /Products/Edit/5 ProductsController (Controller) /Products/Update/5 URL:
  • 10. WHAT’S THE POINT? It’s not WebForms 4.0! It’s an alternative Flexible Extendible in each part of the process Fundamental Only System.Web! (Not WPF or Winforms) Plays well with others NHibernate for models, NVelocity for views, Unity for dependency injection, … Keep things simple!
  • 11. GOALS Maintain Clean Separation of Concerns Easy Testing Red/Green TDD Highly maintainable applications by default Extensible and Pluggable Support replacing any component of system Source available on CodePlex Not open source Build your own, private version
  • 12. DRIVING GOALS Enable clean URLs and SEO SEO friendly URL structures Great ASP.NET integration All providers are supported Membership, Caching, Session, … ASP.NET designer in VS2008 Check my blog! http://tinyurl.com/maarten-outputcache http://tinyurl.com/maarten-mvcsitemap
  • 13. REQUEST LIFECYCLE Browser requests /Products/ Route is determined Controller is activated Method on Controller is invoked Controller does some stuff Renders View, passing in ViewData
  • 14. ACTUALLY, THERE’S MORE… You can replace each step of the process!
  • 16. ROUTING – PRETTY URLS Developer adds Routes to a global RouteTable Mapping creates RouteData – a bag of key/values This is NOT URL rewriting! It’s more! public static void RegisterRoutes(RouteCollection routes) { routes.MapRoute( &quot;Default &quot;{controller}/{action}/{id}&quot;, new { controller = &quot;Home&quot;, action = &quot;Index&quot;, id = &quot;&quot; }, new { controller = @&quot;[^\.]*&quot; } ); }
  • 17. CUSTOM URL ROUTING Can setup custom URL routing rules within the RegisterRoutes() method in Global.asax Enables very flexible mapping and route rules: Default parameters Constraints Named routes Wildcard “catch-all” routes
  • 19. EXTENSIBILITY These are all pluggable! Views Controllers Models Routes Check my blog! http://tinyurl.com/maarten-mvccustomviewengine MvcContrib http://www.codeplex.com/MvcContrib
  • 20. VIEWENGINE, VIEW View engines instantiate views Views render output You get WebForms by default Can implement your own MvcContrib has them for Brail, NHaml, NVelocity, … Customizing these can be used to Offer new DSLs to make HTML easier Generate totally different mime/types Images, RSS, JSON, XML, OFX, VCards, DOCX, whatever public abstract class VirtualPathProviderViewEngine : IViewEngine { protected abstract IView CreatePartialView(...); protected abstract IView CreateView(…); } public interface IView { void Render(ViewContext viewContext, TextWriter writer); }
  • 21. NHAML – FROM… <%@ Page Language=&quot;C#&quot; MasterPageFile=&quot;~/Views/Shared/Site.Master&quot; AutoEventWireup=&quot;true&quot; CodeBehind=&quot;List.aspx&quot; Inherits=&quot;MvcApplication5.Views.Products.List&quot; Title=&quot;Products&quot; %> <asp:Content ContentPlaceHolderID=&quot;MainContentPlaceHolder&quot; runat=&quot;server&quot;> <h2><%= ViewData.CategoryName %></h2> <ul> <% foreach (var product in ViewData.Products) { %> <li> <%= product.ProductName %> <div class=&quot;editlink&quot;> (<%= Html.ActionLink(&quot;Edit&quot;, new { Action=&quot;Edit&quot;, ID=product.ProductID })%>) </div> </li> <% } %> </ul> <%= Html.ActionLink(&quot;Add New Product&quot;, new { Action=&quot;New&quot; }) %> </asp:Content>
  • 22. NHAML – TO… http://code.google.com/p/nhaml/ %h2= ViewData.CategoryName %ul - foreach (var product in ViewData.Products) %li = product.ProductName .editlink = Html.ActionLink(&quot;Edit&quot;, new { Action=&quot;Edit&quot;, ID=product.ProductID }) = Html.ActionLink(&quot;Add New Product&quot;, new { Action=&quot;New&quot; })
  • 23. SPARK – FROM… <%@ Page Language=&quot;C#&quot; MasterPageFile=&quot;~/Views/Shared/Site.Master&quot; AutoEventWireup=&quot;true&quot; CodeBehind=&quot;List.aspx&quot; Inherits=&quot;MvcApplication5.Views.Products.List&quot; Title=&quot;Products&quot; %> <asp:Content ContentPlaceHolderID=&quot;MainContentPlaceHolder&quot; runat=&quot;server&quot;> <h2><%= ViewData.CategoryName %></h2> <ul> <% foreach (var product in ViewData.Products) { %> <li> <%= product.ProductName %> <div class=&quot;editlink&quot;> (<%= Html.ActionLink(&quot;Edit&quot;, new { Action=&quot;Edit&quot;, ID=product.ProductID })%>) </div> </li> <% } %> </ul> <%= Html.ActionLink(&quot;Add New Product&quot;, new { Action=&quot;New&quot; }) %> </asp:Content>
  • 24. SPARK – TO… http://dev.dejardin.org/ <viewdata model=&quot;MvcApplication5.Models.Product“/> <h2>${ViewData.CategoryName}</h2> <ul> <for each=&quot;var product in ViewData.Products&quot;> <li> ${product.ProductName} <div class=&quot;editlink&quot;> (<a href=&quot;/Products/Edit/${product.ProductID}&quot;>Edit</a>) </div> </li> </for> </ul> <a href=&quot;/Products/New&quot;>Add New Product</a>
  • 25. FILTER ATTRIBUTES Enable custom behavior to be added to Controllers and Controller actions 4 types Authentication filter Action filter Result filter Exception filter Examples: [Authorize], [LogAction], [OutputCache], … http://tinyurl.com/maarten-outputcache
  • 27. TESTING Has anyone tried testing webforms? Without IIS being fired up? Each component tested individually?
  • 28. INTERFACES AND TESTING These are all easily mockable! HttpContextBase, HttpResponseBase, HttpRequestBase Extensibility IController IControllerFactory IRouteHandler IViewEngine
  • 29. 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[ &quot; Message &quot; ], &quot; Hello &quot; ); }
  • 30. TESTING CONTROLLER ACTIONS USING MOQ [TestMethod] public void TestInvalidCredentials() { LoginController controller = new LoginController(); var mock = new Mock<System.Web.Security.MembershipProvider>(); mock.Expect(m => m.ValidateUser(&quot;&quot;, &quot;&quot;)).Returns(false); controller.MembershipProviderInstance = mock.Object; var result = controller.Authenticate(&quot;&quot;, &quot;&quot;) as ViewResult; Assert.IsNotNull(result); Assert.AreEqual(result.ViewName, &quot;Index&quot;); Assert.AreEqual(controller.ViewData[&quot;ErrorMessage&quot;], &quot;Invalid credentials! Please verify your username and password.&quot;); }
  • 31. TESTING FRAMEWORKS Any framework is supported! Visual Studio Test NUnit XUnit …
  • 33. DEMO A complete application!
  • 34. SUMMARY A new option for ASP.NET. More control over your <html/> and URLs More easily testable approach Not for everyone – only use it if you want to Shipping later this year
  • 35. COMMON QUESTIONS Should I use WebForms or MVC? Are there any controls? Do I have to write <%= %> code in my view? What about AJAX? How fast/scalable is it? Is it safe? When will it ship?
  • 37. THANK YOU! Make sure to check http://blog.maartenballiauw.be/category/MVC.aspx