Day #1: Learn ASP.Net MVC 5, Adding a Controller

After a long time, I have returned to web development. For the current web development paradigm, the ASP.NET MVC Framework is one of the most popular and heavily-used web development frameworks. Seeing popularity and usefulness of ASP.NET MVC, I have decided to write easy to go blog series on this. In this blog article series, each day I will share my learning in the form of blog posts.

In Day 1, let us learn about Controllers; first in simpler words:

A Controller does all the actions in MVC. It handles user interactions or inputs. It passes user's data to a Model and selects a View to render on the UI.

actions in MVC

In more coding terms, a Controller can be defined as below:
  1. A Controller is a class
  2. It contains one or more methods called Actions
  3. An Action can return a simple string or a selected View to the UI
  4. Action takes all browser requests or user inputs
  5. It retrieves data from the Model
  6. It selects the View to be rendered on the UI

You can add a controller by right-clicking on the Controllers folder in the Solution Explorer. In the context menu select "Add" -> "Controller". There are various types of Controller templates available to be added. Let us proceed to selecting the "MVC 5 Controller - Empty" template to add the Controller.

add controller

Next provide a name to the Controller. I am giving it the name ProductController.
 
name to Controller

In Solution Explorer, you will notice in the Controller folder, the ProductController.cs file was added and in the Views folder, there is also the subfolder Product that was added.
 
Views folder

Next let us go ahead and remove the default code created and following two actions. The first Action is Index (it is default) action. The Index action does not take any input parameter. The second Action added is Welcome. It takes two input parameters id and name.
 

public class ProductController : Controller

{

    //

    // GET: /Product/

    public string  Index()

    {

            return "this is default action";

    }

    public string Welcome(string name, int id)

    {

        return "this is welcome action" + name + "with id " + id;

    }

}

The following points are worth noticing about ProductController.

  1. The ProductController class extends the Controller class
  2. Actions are created as methods.
  3. In this case Actions are returning a string.
  4. Actions can return other types as well. We will discuss them in further posts.
  5. Actions can take zero or more input parameters.

We will now run the application. The default action of the Product Controller is index. On navigating to the Product Controller, the Index action will be called. In this case the Index action is returning a string.

run application

We have put one more action, Welcome, in the Product controller. This takes the two input parameters Id and name. You can call the Welcome action as below. We are passing two input parameters as query strings.
 
input parameters as query string

The better approach to pass input parameters as Route Data than query string. Id is the default input parameter. So instead of passing id as a query string, you can pass it as route data as well.
 
default input parameter

You can find the default route and parameters in RouteConfig.cs.

RouteConfig

In the code above, as you see, the default controller is Home, the default action is Index and the default optional parameter is id. If you want to pass other input parameters as a route option then you can very much do that by adding more routes.
 
adding more routes

We have added a route with name and id as parameter. On running the application, now you can pass name and id as route options.
 
added a route with name and id as parameter

This is the basics of working with Controllers in MVC. In further posts we will get into deeper concepts of Controllers. I hope you find this article useful.
 
Thanks for reading.