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.
In more coding terms, a Controller can be defined as below:
- A Controller is a class
- It contains one or more methods called Actions
- An Action can return a simple string or a selected View to the UI
- Action takes all browser requests or user inputs
- It retrieves data from the Model
- 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.
Next provide a name to the Controller. I am giving it the name ProductController.
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.
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.
- The ProductController class extends the Controller class
- Actions are created as methods.
- In this case Actions are returning a string.
- Actions can return other types as well. We will discuss them in further posts.
- 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.
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.
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.
You can find the default route and parameters in
RouteConfig.cs.
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.
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.
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.