w3resource

Controllers


In the previous tutorial, we explored routing in ASP.NET MVC, in this tutorial, we will take a deep look into the controller part of the MVC framework as regards ASP.NET.

In the ASP.NET MVC, controller classes are derived from the System.Web.Mvc.Controller base class. They contain public methods often referred to Action methods. These action methods handle incoming browser requests, performs the necessary operations demanded by the requests and then returns the appropriate response. The returned response could either be view or a bunch of JSON data.

ASP.NET MVC follows a naming convention, which is that Controller names must end with the suffix "Controller". For example, controller for the home page must go in as HomeController while activity page will be ActivityControlller. In addition to the naming convention, every controller must be located in the controller folder of the MVC project.

Creating a New Controller

Now, let's add a new empty controller in our ActivityTab application which we created in the past tutorial.

Note:The resource error not found error will be thrown, if the suffix "Controller" is not found on the controller class name.

In the Visual Studio, right click on the Controller folder -> select Add -> click on Controller, as illustrated in the diagram below:

asp.net creating a new controller

ASP.NET is known for making developers job easier by scaffolding most of the common repeating process in MVC application development life cycle.

Scaffolding is an automatic code generation. It reduces the time taken to develop a controller, views and models in an MVC framework.

Select your preferred scaffold options from the list of options as shown in the diagram below:

asp.net add scaffold

We will select "MVC 5 Controller - Empty" for now and click Add. Thus will prompt an Add Controller dialog as shown below

asp.net add controller

In the Add Controller dialog, enter the name of the controller. Remember, controller name must end with Controller. Let's enter ActivitiesController and click Add.

This will create ActivitiesController class with Index method in ActivitiesController.cs file under Controllers folder, as shown below.

ActivitiesController.cs

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

namespace ActivityTab.Controllers
{
    public class ActivitiesController: Controller
    {
        public ActionResult Index()
        {
            return View();
        }
    }
}

As you can see above, the ActivitiesController class is derived from Controller class. Every controller in MVC must derived from this abstract Controller class. This base Controller class contains helper methods that can be used for various purposes.

Now, we will return a dummy string from Index action method of above ActivitiesController.

Change the return type of Index method from ActionResult to string and return dummy string is shown below.

In the upcoming sections, we will learn more about ActionResult and other return types available and when to use them.

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

namespace ActivityTab.Controllers
{
    public class ActivitiesController: Controller
    {
        public string Index()
        {
                return "Welcome to the index method of the Activity controller"
        }
    }
}

In the routing tutorial, we saw the make ups of the ASP.NET routing protocol. We learnt that the request to the URL http://localhost/activities or http://localhost/ activities /index is handled by the Index() method of ActivitiesController class, shown above.

Now run the application, and navigate to the above URL on your browser to see our activity controller in action.



Inviting useful, relevant, well-written and unique guest posts