Introduction
This article describes creation of MVC Web Sites. As you know, there is no option available to create the MVC Web Sites in the Visual Studio 2013.
In that context, using ASP.NET Frameworks (3.5 and above), Visual Studio allows you to create the ASP.NET Web Applications with the MVC Project Templates. This article explains that the model and controllers are defined in the App_Code folder and the web form takes place of the view. The Global.asax file is to be used to define the route maps.
So let's proceed to create the website with the following procedure.
Step 1: Open Visual Studio IDE.
Step 2: Create the ASP.NET Empty Web Site.
Step 3: Open the Web.Config file and modify it with the following code:
<configuration>
<system.web>
<compilation debug="true" strict="false" explicit="true" targetFramework="4.5">
<assemblies>
<add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</assemblies>
</compilation>
<httpRuntime targetFramework="4.5"/>
<pages>
<namespaces>
<add namespace="System.Web.Mvc"/>
<add namespace="System.Web.Mvc.Ajax"/>
<add namespace="System.Web.Mvc.Html"/>
<add namespace="System.Web.Routing"/>
</namespaces>
</pages>
</system.web>
</configuration>
Step 4: Add class named CricketerModel by right-clicking in your website.
Step 5: Replace the boilerplate code with the following code:
using System;
public class CricketerModel
{
public int ID { get; set; }
public string Name { get; set; }
public string Team { get; set; }
public CricketerModel() { }
public CricketerModel(int CricId, string CricName, string CricTeam)
{
this.ID = CricId;
this.Name = CricName;
this.Team = CricTeam;
}
}
Step 6: Add another class named CricketerController and replace the code with the following code:
using System.Collections.Generic;
using System.Web.Mvc;
public class CricketerController : Controller
{
public ActionResult List()
{
List<CricketerModel> CricItems = new List<CricketerModel>();
CricItems.Add(new CricketerModel(1, "Sachin", "India"));
CricItems.Add(new CricketerModel(2, "Ricky", "Australia"));
CricItems.Add(new CricketerModel(3, "Sanath", "SriLanka"));
CricItems.Add(new CricketerModel(4, "Wasim", "Pakistan"));
ViewData["CricList"] = CricItems;
return View("~/CricketerView.aspx");
}
}
Step 7: Add a Global.asax file to your web site by right-clicking on your website and adding a new item as in the following:
globalfile image
Step 8: Find the Application_Start() and paste the following code into it:
RouteTable.Routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
RouteTable.Routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new
{
controller = "CricketerController",
action = "List",
id = UrlParameter.Optional
}
);
Step 9: Add a WebForm named CricketerView and replace the code with the following code:
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Cricketer View</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<th>Cricketer ID</th>
<th>Cricketer Name</th>
<th>Cricketer Team</th>
</tr>
<%
List<CricketerModel> CricItems = (List<CricketerModel>) ViewData["CricList"];
%>
<% foreach (var item in CricItems)
{ %>
<tr>
<td><%= item.ID %></td>
<td><%= item.Name %></td>
<td><%= item.Team %></td>
</tr>
<% } %>
</table>
</div>
</form>
</body>
</html>
We have created the CricketerView to display the contents of our website. Now we are about to use our MVC Web Site. So, proceed to the next section.
Using Visual Studio Template
Now just use the following procedure to use the website as a Visual Studio Template:
Step 1: "File" -> "Export Template"
Step 2: As you can see, the options are filled in as a default, if not then please select the option.
Step 3: Describe the template (Optional).
Step 4: Check out the Template after restarting Visual Studio.
Summary
This article will help you to create and work with your Visual Studio Template. It also describes the creation of the MVC Web Sites that is generally available for the ASP.NET Web Applications. Thanks for reading.