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

Description: This Mind Map that explains your first ASP.NET MVC "Hello World" Program Visit my blog at 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 } "{controller}/{action}/{id}" is a generic route used for many web requests {controller}, {action} & {id} are route tokens understood by the MVC framework The ignore route tells MVC to ignore requests for HTTP Handlers like Trace.axd

1.2. HomeController.cs (Controller)

1.2.1. Controller Implementation Need to Implement IController & its Execute Method Either you implement IController directly, or derive from Controller or another super type 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();   } } HomeController derives from the base class Controller Index() is the "action" method ViewData is a Dictionary object used for passing information to the View Index returns ActionResult which renders HTML to the browser

1.2.3. Action Method Implementation Public methods called implicitly or explcity based on URL & routes The Controller (via the action method) populates the ViewData with the model data to be rendered Primary object to be displayed by the view should be set via Model property of ViewData It could also simply be passed directly to the view View(obj) << This will set the Model property to obj ViewData is forwarded to the view by calling View() View() should return an ActionResult object 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 A view can have a master page 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 Implicit View Location >> .aspx view file under Views/<controller name>/<action name> 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> Master Page is a .master file No code-behind exists for the master page Html.ActionLink is a view helper Html.RenderPartial renders a partial view 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 Home Index.aspx About.aspx

2.2.3. Controller HomeController.cs

2.2.4. Scripts

2.2.5. Content 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 Microsoft has wrapped key ASP.NET code in abstract classes to improve testability Abstract classes loosens coupling making way for easy unit testing Prior to this, HttpContext class caused problems for testing due to it sealed and static members HttpResponseBase HttpRequestBase 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