ASP.NET MVC - "Hello, World!" Program

Description: This Mind Map that explains your first ASP.NET MVC "Hello World" Program Visit my blog at http://preetiedul.wordpress.com/bottom-of-the-stack/ to view other Mind Maps

Get Started. It's Free
or sign up with your email address
ASP.NET MVC - "Hello, World!" Program by Mind Map: ASP.NET MVC - "Hello, World!" Program

1. Key Classes

1.1. Global.asax (Route)

1.1.1. Global.asax is used to define & register routes

1.1.2. A route is a complete definition of how to dispatch a web request to a controller (using System.Web.MvcRouteHandler)

1.1.3. Need to register routes before any request is processed

1.1.4. Before ASP.NET MVC, ISAPI filters & HTTP Modules were needed for URL Routing

1.1.5. With ASP.NET MVC, routes are first class citizens and can be managed directly in the Web Application

1.1.6. protected void Application_Start() {   RegisterRoutes(RouteTable.Routes); } public static void RegisterRoutes(RouteCollection routes) {   routes.IgnoreRoute("{resource}.axd/{*pathinfo}");   routes.MapRoute(     "Home", // route name     "{controller}/{action}/{id}" // url with parameters     "new {controller = "Home", action="Index", id=""}); // default value }

1.1.6.1. "{controller}/{action}/{id}" is a generic route used for many web requests

1.1.6.2. {controller}, {action} & {id} are route tokens understood by the MVC framework

1.1.6.3. The ignore route tells MVC to ignore requests for HTTP Handlers like Trace.axd

1.2. HomeController.cs (Controller)

1.2.1. Controller Implementation

1.2.1.1. Need to Implement IController & its Execute Method

1.2.1.2. Either you implement IController directly, or derive from Controller or another super type

1.2.1.3. Every Controller contains some Action method implementations e.g. public ActionResult Index()

1.2.2. [HandleError] public class HomeController : Controller {   public ActionResult Index()   {     ViewData["Message"] = "Welcome to ASP.NET MVC!";     return View();   } }

1.2.2.1. HomeController derives from the base class Controller

1.2.2.2. Index() is the "action" method

1.2.2.3. ViewData is a Dictionary object used for passing information to the View

1.2.2.4. Index returns ActionResult which renders HTML to the browser

1.2.3. Action Method Implementation

1.2.3.1. Public methods called implicitly or explcity based on URL & routes

1.2.3.2. The Controller (via the action method) populates the ViewData with the model data to be rendered

1.2.3.2.1. Primary object to be displayed by the view should be set via Model property of ViewData

1.2.3.2.2. It could also simply be passed directly to the view View(obj) << This will set the Model property to obj

1.2.3.3. ViewData is forwarded to the view by calling View()

1.2.3.4. View() should return an ActionResult object

1.2.3.5. Via the ActionResult object, we are asking the framework to render the view to the browser

1.3. Index.aspx (View)

1.3.1. A view is simply an ASPX page

1.3.1.1. A view can have a master page

1.3.1.2. No code behind is necessary for a view in MVC (however, since MVC uses ASP.NET webform template engine, you can add code-behind to views)

1.3.2. MVC uses convention (over configuration) to locate views

1.3.2.1. Implicit View Location >> .aspx view file under Views/<controller name>/<action name>

1.3.2.2. Explicit View Location >> passed into the View() method call

1.3.3. A view only decides HOW to render the data it has been passed. It should not make any decisions regarding WHAT data should be rendered.

1.3.4. <!-- INDEX.ASPX --> <%Page MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage"%> <asp:content id="indextitle" contentPlaceHolderId="TitleContent" runat="server">   Home Page </asp:content> <asp:content id="indexcontent" contentPlaceHolderId="MainContent" runat="server">   <h2> <%= Html.Encode(ViewData["message"]) %> </h2> </asp:content>

1.3.5. <!-- MASTER PAGE --> <%Page Inherits="System.Web.Mvc.MasterViewPage"%> <html runat="server"> <head>   <title><asp:ContentPlaceHolder="TitleContent" runat="server"/></title>     <link href="../../content/site.css" rel="stylesheet" type="text/css" /> </head> <body>   <% Html.RenderPartial("LogOnUserControl") %>    <%= Html.ActionLink("Home", "Index", "Home") %>       <asp:ContentPlaceHolder ID="MainContent" runat="server" /> </body> </html>

1.3.5.1. Master Page is a .master file

1.3.5.2. No code-behind exists for the master page

1.3.5.3. Html.ActionLink is a view helper

1.3.5.4. Html.RenderPartial renders a partial view

1.3.5.5. MVC has multiple view helpers to assist dynamic html generation

2. Easy Steps !

2.1. In Visual Studio select ASP.NET MVC Web Application template

2.1.1. Starter kits (templates) are available that can automatically plug MvcContrib, NHibernate, NUnit, RhinoMocks etc into your project

2.1.2. An "Empty" template is also available in VS that does not add the sample files to your project

2.2. Visual Studio will automatically create the MVC folder structure for the project

2.2.1. Model

2.2.2. View

2.2.2.1. Home

2.2.2.1.1. Index.aspx

2.2.2.1.2. About.aspx

2.2.3. Controller

2.2.3.1. HomeController.cs

2.2.4. Scripts

2.2.5. Content

2.2.5.1. Site.css

2.2.6. global.asax

2.3. Run the application & you should see the welcome screen!

2.3.1. ASP.NET MVC - Hello, World!

3. Unit Testing

3.1. Controller

3.1.1. Easily tested as it is de-coupled from ASP.NET runtime & the view

3.1.2. Add a test method for every action method in your controller

3.2. Routes

3.2.1. Can be unit tested in MVC

3.2.2. Need to mock some classes like HttpContext for testing routes

3.2.2.1. Microsoft has wrapped key ASP.NET code in abstract classes to improve testability

3.2.2.2. Abstract classes loosens coupling making way for easy unit testing

3.2.2.3. Prior to this, HttpContext class caused problems for testing due to it sealed and static members

3.2.2.4. HttpResponseBase

3.2.2.5. HttpRequestBase

3.2.2.6. HttpContextBase

3.2.3. Support from frameworks like MvcContrib is necessary to make unit testing easy & intutive

3.3. View

3.3.1. Not very easy to unit test

3.3.2. Needs to be tested using Selenium / Watir / Watin

3.3.3. It is important to keep "logic" out of views, thereby reducing the possibility of a bug

3.4. NUnit is the preferred unit testing framework

3.5. Visual Studio will automatically add a Unit Test (MSTest) project for you

3.6. [TestClass] public class HomeControllerTest {   [TestMethod]   public void Index()(   {     // Given     HomeController controller = new HomeController();     // When     ViewResult result = controller.Index() as ViewResult;     // Then     ViewDataDictionary viewData = result.ViewData;     Assert.AreEqual("Welcome to ASP.NET MVC!", viewData["message"]);   } }

3.7. It follows the arrange-act-assert flow for unit tests (given-when-then)

4. About

4.1. Description: This Mind Map that explains your first ASP.NET MVC "Hello World" Program

4.2. Created by: Preeti Edul

4.3. Visit my blog for more ASP.NET MVC mind maps http://preetiedul.wordpress.com/bottom-of-the-stack/