There’s nothing like a page in an ASP.NET MVC application and nothing directly corresponds to a page when you specify a URL path. The closest thing to an ASP.NET MVC application page is called the View.

In the ASP.NET MVC application, the controller handles all incoming browser requests and maps these requests to control actions. A control action can return a view or it can also perform some other kind of action, such as redirecting to another control action.

Let’s look at a simple View example by setting up a new ASP.NET MVC project.

Step 1 − Open Visual Studio and then click File, then Project menu.

Step 2 − Select templates from the left panel, then Visual C # and then Web.

Step 3 − Select ASP.NET Web Application in the middle panel.

Step 4 − In the Name field, enter the projectname’ MVCViewDemo’ and click Ok to continue. You will see the following dialog asking you to set the initial ASP.NET project content.

Step 5 − To keep things simple, select the empty option and select the checkbox MVC in the section ‘ Add folders and core references’ and click Ok.

A basic MVC project with minimally predefined content will be created. We must now add the controller.

Step 6 − In the solution explorer, right- click the controller folder and select Add Controller.

Step 7 − Choose the MVC 5 Controller– Empty option and then click’ Add.’

Step 8 − Set the HomeController name and press the Add button.

You will see a new C # file’ HomeController.cs’ in the Controllers folder that is also available to edit in Visual Studio.

Update the file HomeController.cs, which contains two methods of action as shown in the following code.

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

namespace MVCViewDemo.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            return View();
        }
        public string Mycontroller()
        {
            return "Hi, I am a controller";
        }
    }
}

Step 9 − Run this app and append/Home/MyController to the browser URL and press Enter. You will receive the next output.

Since the MyController action simply returns the string, we must first add a View from the action.

Step 10 − Before you add a view, add another action that returns the default view.

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

namespace MVCViewDemo.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            return View();
        }
        public string Mycontroller()
        {
            return "Hi, I am a controller";
        }
        public ActionResult MyView()
        {
            return View();
        }
    }
}

Step 11 − Run this app and append/Home/MyView to the browser URL and press Enter. You will receive the output below.

You can see here that we have an error and that error is actually quite descriptive, which tells us that the MyView view can not be found.

Step 12 − To add a view, click right within the MyView action and select Add view.

It will show the Add View dialog and the default name will be added.

Step 13 − Check the checkbox ‘ Use a layoutpage’ and clickthe’ Add’ button.

Step 14 − In this view add some text using the following code.

<div>
     Hi, I am a view
</div>

Step 15 − Run this application and apend /Home/MyView to the URL in the browser. Press enter and you will receive the following output.

Subscribe to our newsletter

You can unsubscribe anytime you want