Controllers in ASP.NET MVC, Controllers in ASP.NET, Controllers in MVC, Controllers in ASP
Hello friends, I hope you all are doing great. In today's tutorial, I am going to give you a detailed overview of Controllers in ASP.NET MVC. It's the fourth article in ASP Web App Series. In our Previous tutorial, we have had a look at Views in ASP.NET MVC and in that tutorial, we have mentioned this Controller a lot and today we are gonna discuss it in detail. Controllers acts as a messenger in ASP.NET MVC Web App, it takes message or data from place to another. I have also shared a video at the end of this tutorial and I would suggest you to watch it once to get better understanding of controllers. So, let's get started with Controllers in ASP.NET MVC:

Controllers in ASP.NET MVC

  • Controller is the back bone of any ASP.NET MVC Web App as it acts as a messenger between different entities.
  • When use hit your website address in the browser then browser generates an HTTP request, which is received by the server and in ASP case it comes to Controller.
  • Controller in ASP.NET handles the coming HTTP request and then selects the respective Model in ASP.NET MVC and then gets data from the SQL Database, if required.
  • Along with this data from Model, it forwards the command to respective View, which in turn opens up in front of the user.
  • So, you must have got the importance of Controllers as they are kind of doing the management work, getting and assigning commands & data.
  • Controllers are inherited from System.Web.Mvc.Controller and are simple C# classes.
  • You can find the Controllers folder in Solution Explorer, click to expand, as shown in below figure:
Controllers in ASP.NET MVC, Controllers in ASP.NET, Controllers in MVC, Controllers in ASP
  • You can find 3 Controller files in this folder, named as:
    • AccountController.cs
    • HomeController.cs
    • ManageController.cs
  • So, now click on the Home Controller file and let's have a look at the code, shown in below figure:
Controllers in ASP.NET MVC, Controllers in ASP.NET, Controllers in MVC, Controllers in ASP
  • In the above code, we have used a method named ActionResult, which will send the data to View.
  • After the name of method, the name of each page is placed present in the Views > Home folder.
  • Now let's open the About Page and the string variable is shown on the About Page, as in below figure:
Controllers in ASP.NET MVC, Controllers in ASP.NET, Controllers in MVC, Controllers in ASP
  • If you open the About View file, then you will see this string variable is placed there, that's why we have it on screen.
  • We can create unlimited controllers and can use them for different purposes.
  • Here's a video which will give better understanding of Controllers in ASP.NET MVC:
So, that was all about Controllers in ASP.NET MVC. I hope now you can easily understand the difference between Models, Views & Controllers. In coming lectures, we will have a look at How to create New Models, Views & Controllers in ASP.NET MVC. Take care & have fun !!! :)