Online Mind Mapping and Brainstorming

Create your own awesome maps

Online Mind Mapping and Brainstorming

Even on the go

with our free apps for iPhone, iPad and Android

Get Started

Already have an account? Log In

ASP.NET MVC - "Hello, World!" Program by Mind Map: ASP.NET MVC -
"Hello, World!"
0.0 stars - reviews range from 0 to 5

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

Key Classes

Global.asax (Route)

Global.asax is used to define & register routes

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

Need to register routes before any request is processed

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

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

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

HomeController.cs (Controller)

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()

[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

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

Index.aspx (View)

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)

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

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.

<!-- 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>

<!-- 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

Easy Steps !

In Visual Studio select ASP.NET MVC Web Application template

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

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

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


View, Home, Index.aspx, About.aspx

Controller, HomeController.cs


Content, Site.css


Run the application & you should see the welcome screen!

ASP.NET MVC - Hello, World!

Unit Testing


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

Add a test method for every action method in your controller


Can be unit tested in MVC

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

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


Not very easy to unit test

Needs to be tested using Selenium / Watir / Watin

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

NUnit is the preferred unit testing framework

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

[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"]);   } }

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


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

Created by: Preeti Edul

Visit my blog for more ASP.NET MVC mind maps