Start With The New Taste of MVC 5

Introduction

Microsoft released the latest version of MVC, MVC 5. MVC 5 was released with Visual Studio 2013 Preview. In this article I am introducing you to creating an ASP.NET Web Application with the latest MVC Project Template with which you will learn the basics of MVC 5.

In that context, here I am creating a MVC 5 Web Application in which I introduce you to all the aspects of MVC 5. As you know, MVC stands for Model View Controller and here we also create a new controller for the web application.

Prerequisites

The following are the prerequisites to start the MVC 5 application:

  • Visual Studio 2013 Preview or later
  • ASP.NET Web Tools for Visual Studio 2013
  • NuGet Package Manager for Visual Studio 2013

Create Application in MVC 5

So let's start to create an ASP.NET Web Application with the MVC 5 Project template. Follow the procedure given below.

Step 1: Open Visual Studio 2013. (I am using Visual Studio 2013 RC).

Visual Studio Start Page

Step 2: Click on "New Project" to create a web application.

Create Web Application

Step 3: Select the MVC template.

Mvc Template

Step 4: Click on "Create Project".

When you click on Create Project, Visual Studio automatically creates a MVC web application in which you can see the folders like Models, Controllers and Views. There are two files named Account and Home present in the Controller and View folders. As you can see in the following figure the HomeController.cs file has the corresponding view in the Views folder.

Controller in MVC5

Debug Application

Now, you have created an application, so its time to debug the application. Press F5 to debug your application. Visual Studio allows IIS Express to debug the application. The browser will open and show the application's home page.

Home Page in MVC5

In the browser's URL, you will see "localhost" and a port number. Localhost represents that you are running it on local computer and port number generated by Visual Studio that is used for the web server.

Working with Controller

As you know, there are various controllers already present in the application. So, let's start to create a new controller with the following procedure.

Step 1: Right-click on your Controller folder.

Create Controller in MVC5

Step2: Click on "Add Scaffold".

Add Scaffold in MVC5

Step 3: In the next wizard, enter your Controller name as "MYCrikceters".

Add Controller in MVC5

Visual Studio will create the MYCricketersController.cs file and display your new controller.

Controller

Step 4: Modify your code with the following code:

using System.Web.Mvc;

 

namespace MvcCricket.Controllers

{

    public class MYCricketersController : Controller

    {

        // GET: /MYCricketers/

        public string Index()

        {

            return "Hello this is  My First Controller";

        }

 

        public string Welcome()

        {

            return "Welcome to My Cricketer Controller";

        }

     }

}

There are two controller methods used in here. These methods will return a string.

Step 5: Debug your application,

Home Page in MVC5

Visual Studio by default opens HomeController. You need to provide your controller name with the localhost like localhost:1234/MYCricketers. Enter your controller name with the localhost. In here, MYCricketersController is the first method named as Index.

First Controller

Similarly open the Welcome method that will open by providing it with the URL as shown below:

Welcome Controller

Summary

This article introduced you to a web application based on the MVC 5 project template. The controllers used in here displayed directly in the browser but normally Views are created to display the controllers. So, just start with the new MVC 5 application. Thanks for reading.


Similar Articles