Reader Level:
ARTICLE

Develop and test an appliction in ASP.NET MVC

Posted by Prashant Chaudhary Articles | ASP.NET MVC December 02, 2011
In this article you will learn to develop an application and test it with the help of TDD in ASP.NET MVC.
  • 0
  • 0
  • 3742

As we know MVC, gives Test Driven Development hence use this in a small application.

Step 1 : First Open Visual Stdio 2010>New> Project.

1.gif


Step 2 : Chose ASP.NET MVC Web Application.

2.gif

Step 3 : After that next step is to check the Radio Button TO test the development.

3.gif

Step 4 : Now you can see the following items in your solution explorer.


4n.gif
Step 5 : Now the First step is to form a path to the Controller, Model and Veiw in Global.asax file as shown in the Solution Explorer above

using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.Mvc;
using
System.Web.Routing;

namespace MvcApplicationByPC
{

    // Note: For instructions on enabling IIS6 or IIS7 classic mode,

    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication

    {

        public static void RegisterRoutes(RouteCollection routes)

        {

            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        protected void Application_Start()

           {
      routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

   routes.MapRoute(

    "PacificShow",                    // Route name

    "Pacific/{firstname}",            // URL with parameters

  new {                             // Parameter defaults

        controller = "Pacific",

        action = "Show",

        firstname = ""

   } 
    );
   
}
 
         AreaRegistration.RegisterAllAreas(); 
          RegisterRoutes(RouteTable.Routes);
      }
          }
              }

Step 6 :Now after manipulating the routing add Model Class by following the steps as shown below:

  1. Right Click on Model>add>new item>chose class from template

  2. Name the class and click on add button
Now code this class to get and set the data which you want:

using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
namespace
MvcApplicationByPC.Models
{
    public class Pacific
    {
        public string Firstname { get; set; }
        public string Mcnid { get; set; }
        public string Technology { get; set; }
        public string Address { get; set; }
    }
}

Step 7 : Next step is to add Controller who route is mentioned in the route map.

Step to add Controller

Right Click on Controller Folder>add Controller>name the Controller

5.gif

Now work on Controller to pass the data:

using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.Mvc;
using
MvcApplicationByPC.Models;

namespace MvcApplicationByPC.Controllers
{

    public class PacificController : Controller

    {

        //

        // GET: /Pacific/

        public ActionResult Show (string firstname)

        {

            if (string.IsNullOrEmpty(firstname))

            {

                ViewData["Errormessage"] = "Please enter the first name";

            }

            else

            {

               Pacific mcn = new Pacific

               {

                         Firstname = "Pacific",
                    Mcnid = "mcnid",
                    Technology = "Technology",

                    Address = firstname + "-abc"

                };

                ViewData["Firstname"] = mcn.Firstname;

                ViewData["Mcnid"] = mcn.Mcnid;

                    ViewData["Technology"] = mcn.Technology;
                ViewData["Address"] = mcn.Address;

           }
        return View();

            }

        }

    }

Step 8 :After working on Controller and Model final step is to use the third part of MVC that is View

To do so first add View by following these steps:

  1. Right Click on Controller page>add View

  2. Chose View Data Class
6.gif

Now Manipulate the Show.aspx i.e View page

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<
asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
</
asp:Content>
<
asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<%
if (ViewData["ErrorMessage"] != null) { %>
    <h1><%=ViewData["ErrorMessage"]%></h1>

    <% } else { %>
    <h1><%=ViewData["Firstname"]%> <%=ViewData["Mcnid"]%></h1>

    <p>

        Technology: <%=ViewData["Technology"]%>

       </p>

       <p>
        Address: <%=ViewData["Address"]%>
        </p>

<%
} %>

</
asp:Content>

Step 9 : After Creating the View you can run your application by f5

inn.gif

Step 10 : As finally we want to test therefore we have to add test to Controller by doing Right click on Controller folder in Solution Explorer >unit test


7.gif

Step 11 : Finally Chose the Controller which you want to test as shown

8.gif

COMMENT USING

Trending up