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.
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.
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.
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.
View All