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:
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:
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:
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 !!! :)
syedzainnasir
I am Syed Zain Nasir, the founder of The Engineering Projects (TEP). I am a
programmer since 2009 before that I just search things, make small projects and now I am sharing my
knowledge through this platform. I also work as a freelancer and did many projects related to
programming and electrical circuitry. My Google Profile+Follow
Get Connected
Comments on ‘’ Controllers in ASP.NET MVC ‘’ ( 0 )