Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Introduction To ASP.NET MVC
1. The ASP.NET MVC Framework Jess Chadwick Independent Consultant Microsoft MVP, ASPInsider
2. We thank the following companies for their gracious sponsorship Platinum Sponsors Gold Sponsor
3. Agenda What is ASP.NET MVC? Overview / Goals Quick demo The Pattern AJAX-ifying your MVC app Testability Advanced Features
4. What is ASP.NET MVC? Microsoft’s ASP.NET implementation of the MVC software pattern More control over your HTML and URLs More easily testable framework A new Web Project type for ASP.NET An option / alternative
6. What’s the Point? This is not “Web Forms v.Next” All about alternatives Flexibility Extend it… or not Create your own Controller- and View- Engines or use others such as Brail or NHaml Fundamental Part of System.Web namespace Fully supported KISS & DRY
7. Driving Goals Separation of Concerns Easy testing & TDD Highly-maintainable applications Extensible and Pluggable Plug in what you need Build your own custom build
8. Driving Goals (cont’d) Clean URLs and HTML SEO and REST friendly Great interaction with ASP.NET Handlers, Modules, Providers, etc. still work .ASPX, .ASCX, .MASTER pages Visual Studio ASP.NET Designer surface
11. The Model “The center of the universe” This represents your core business domain…AKA – your “bread and butter” Preferably independent of any specific technology
22. ASP.NET MVC vs. Web Forms Data View(ASPX)--------------Controller (Code-Behind) ASP.NET Web Forms (Page Controller) Model List View Detail View Product Controller ASP.NET MVC (Front Controller) Data Model
30. URL Routing Developers add Routes to a global RouteTable Mapping creates a RouteData - a bag of key/values routes.MapRoute( "blog/bydate/{year}/{month}/{day}", new { controller = “blog”, action = “show” }, Constraints = new RouteValueDictionary { {"year", @"{1.4}"}, {"month", @"{1.2}"}, {"day", @"{1.2}"}} })
39. Designed for Testability MockableIntrinsics HttpContextBase, HttpResponseBase, HttpRequestBase Extensibility IController IControllerFactory IRouteHandler ViewEngineBase
40. Testing Controller Actions No requirement to test within ASP.NET runtime! Use RhinoMocks, TypeMock, Moq, etc. Create Test versions of the parts of the runtime you want to stub [TestMethod] public void ShowPostsDisplayPostView() { TestPostRepository repository = new TestPostRepository(); TestViewEngineviewEngine = new TestViewEngine(); BlogController controller = new BlogController(…); var result = controller.ShowPost(2) as ViewResult; Assert.AreEqual("showpost",result.ViewName); Assert.IsTrue(repository.GetPostByIdWasCalled); Assert.AreEqual(2, repository.LastRequestedPostId); }
48. ViewEngineBase View Engines render output You get WebForms by default Can implement your own MVCContrib has ones for Brail, Nvelocity NHaml is an interesting one to watch View Engines can be used to Offer new DSLs to make HTML easier Generate totally different mime/types Images, RSS, JSON, XML, OFX, VCards, whatever.
49. Example View: Web Forms <%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" AutoEventWireup="true" CodeBehind="List.aspx" Inherits="MvcApplication5.Views.Products.List" Title="Products" %> <asp:ContentContentPlaceHolderID="MainContentPlaceHolder" runat="server"> <h2><%= ViewData.CategoryName %></h2> <ul> <% foreach (var product in ViewData.Products) { %> <li> <%= product.ProductName %> <div class="editlink"> (<%= Html.ActionLink("Edit", new { Action="Edit", ID=product.ProductID })%>) </div> </li> <% } %> </ul> <%= Html.ActionLink("Add New Product", new { Action="New" }) %> </asp:Content>
50. Example View: NHaml %h2= ViewData.CategoryName%ul - foreach (var product in ViewData.Products) %li = product.ProductName .editlink = Html.ActionLink("Edit", new { Action="Edit", ID=product.ProductID }) = Html.ActionLink("Add New Product", new { Action="New" })
51. Example View: Spark <h2>${CategoryName}</h2> <viewdata products="IEnumerable[[Product]]"/> <ul if="products.Any()"> <li each="var p in products">${p.Name}</li> </ul> <else> <p>No products available</p> </else>
53. What’s the Point? This is not “Web Forms v.Next” All about alternatives Flexibility Extend it… or not Create your own Controller- and ViewEngines, or use others such as Brail or NHaml Fundamental Part of System.Web namespace Fully supported KISS & DRY