Day #1: Learn ASP.NET MVC 5- Adding Controller


After long time, I am back to web development. At current web development paradigm, ASP.NET MVC framework is one of the most popular and heavily used web development framework. Seeing popularity and usefulness of ASP.NET MVC, I have decided to write easy to go blog series on this. In this blog post series, each day I will share my learning in form of blog posts.

In day 1, let us learn about Controller. Very first in simpler words,

“Controller does all the actions in MVC. It handles user interactions or inputs. It passes users’ data to Model and selects a View to render on UI”


In more coding terms, Controller can be define as below,

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

You can add a controller by right clicking on Controllers folder in solution explorer. In context menu select Add->Controller. There are different types of Controller templates are available to add. Let us go ahead and select MVC 5 Controller- Empty template to add controller.


Next give name to Controller. I am giving name as ProductController.


In solution explorer, you will notice In Controller folder, ProductController.cs file being added and in Views folder, there is subfolder Product being added.


Next let us go ahead and remove default code created and following two actions. First Action is Index (It is default) action. Index action does not take any input parameter. 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;

Following points are worth noticing about ProductController.

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

Let us go ahead and run application. Default action of Product controller is index. On navigating Product Controller, Index action will be called. In this case Index action is returning a string.


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


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


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




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


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


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

3 responses to “Day #1: Learn ASP.NET MVC 5- Adding Controller”

  1. […] Day #1: Learn ASP.NET MVC 5- Adding Controller (Dhananjay Kumar) […]

  2. Helpful for absolute beginners like me…! waiting for more 🙂

  3. […] Day #1: Learn ASP.NET MVC 5- Adding Controller […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at