#35 Controllers in ASP NET | Controllers & IActionResult | ASP.NET Core MVC Course

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this lecture we are going to learn what is a controller and how to use a controller but before that let's first understand why do we need a controller when we Define a route like this it is not a good practice what we are doing here is we are defining a route and then when a URL matches this route from within this middleware function we are returning some response and in this way we have created multiple routes here as you can see and this program.cs file is already bulky now because of these many routes which we have defined here and if we are creating a large Enterprise application we might have hundreds of pages for which by T2 Define a route if we Define those routes in the same program.cs file the program.cs file will become huge and it will be very difficult to maintain it another problem will be when we want to return HTML content or Json content from these middleware functions which we have defined for the routes so basically these middleware functions currently we are only returning some text content from these middleware functions when a route is matched but let's say if you want to return some HTML content or some Json content it will again make this program.cs file increase in the number of lines of code and this is not a good practice there will be readability and maintenance issues and to solve this problem we can use a concept of creating controllers for handling incoming requests a controller is a class that contains a set of action methods and each action method in a controller acts as an endpoint the end point or the action methods are grouped together in a logical way using controller for example let's say we want to have two endpoints or two action methods one for user registration and another for user login now both of these actions are related to user account maintenance right so that is The Logical connection between these two endpoints so we can group these actions together inside a single controller and we may call that controller as account controller so in the account controller we will be defining related endpoints or related action methods like user registration user sign in user sign out Etc so by definition a controller is a collection of action methods and you can also call Action methods as action in short and each action method performs a specific operation based on the input that are passed to it so grouping of actions based on a purpose is the main reason to create controllers and that's not the only thing a controller does a controller is also responsible for properly handling the incoming request creating responses and sending it back to the client but we will talk about it in detail when we have learned about models and views okay let's now go ahead and create a controller and an action inside it for that let's go ahead and let's create a new asp.netco empty project so here the template is already selected let's click next and here let's provide the project name so I will simply call it asp.net controllers okay and we want to create this project inside this C colon asp.net core folder let's click on this next button here I will uncheck this checkbox and I'll select dot net 7. and let's click on this create button so the project has been created here let's open this solution Explorer and here we have our program.cs file now generally we place all our controllers inside a separate folder this folder name can be anything but by convention we call it as controller so here for this project let me go ahead and let me add a new folder and I will call it controller and inside this controller folder we are going to create a new controller and there are several ways you can create a controller so for example when you click on this folder and when you select this ad here you can see this controller option so when you select this using this option you can create a controller but since we are learning everything from scratch I am going to create a class instead of selecting this controller I will select a class option basically a controller is nothing but a class here I am going to call this class as home controller so it is a convention that in your controller class you suffix controller after the controller name so in our case the controller name is going to be home and we are suffixing it with this controller and by default it is a CS file so if I don't specify dot CS extension here and if I click on this add button it will create a DOT CS file and here you can see we have this home controller class always remember that in the class name this controller is important if you don't specify this controller in that case this class is just a ordinary class but we want asp.net code to treat this class as a controller class for that we need to suffix this controller name in this case it is home with controller like this okay so when you suffix controller to your class name asp.net knows that this class is a controller class and it should be treated like a controller and it should be able to handle the request and send the responses now once the request is received by the controller we might want to do something we might want to execute some logic when the request is received by the controller for that we need to define a method inside the controller remember that public methods of a controller are called as action methods or action in short so inside this controller class we are going to Define an action and an action should always be public okay so as I mentioned all the public methods of a controller class is called as actions so here I am going to create a method and it is going to return a value of type string and I will simply call it index and from within this method I am going to return some string value so maybe welcome from asp.net Co application so here we have created a controller inside the controller we have created an action now let's go ahead and let's run this application you see we are seeing this response hello world now this response is coming from program.cs file so if I go to this program.cs file here you will see that for the root URL we are returning this text response hello world so that's why when we have typed root URL in the address bar root URL is basically localhost colon port number so when we are typing this root URL we are seeing this response now this is not required here so I will simply remove it now let me run this application again and now you will see that we have this page not found message here we have this 404 error that's okay because now we are not handling this root URL now here after this root URL let me specify the controller name which in our case is home and then the action name which in our case is index and if I press enter we are still seeing this 404 page not found message that's because here we have created our home controller and this index action method but asp.net core application is currently not aware about this home controller and its index method so we need to register this home controller to the asp.net application for that we need to add this home controller as a service class so that it can participate in dependence injection and we can add any service before calling this build method so here we are calling this build method on this Builder before this on the Builder we can call services and in there we can add any service generally a service class is simply a reusable class which performs certain tasks which can be reused anywhere in the application without having direct relationship with the UI in large applications we create services for business logic but we will dive deep into services in the latter part of this course remember that in asp.netco the controllers are also treated as a service and we need to register all our services on this Builder object before we can use them so here on this service let's go ahead and let's use add transient okay and there let's specify our controller name so our controller name is home controller okay and in order to use this home control we also need to import this namespace because if I go to this home controller class you will notice that we have defined this home controller class inside this namespace so we need to import it inside this program.cs so this is generally how we add a controller as a service in asp.net core application but in larger projects you might have hundreds or thousands of controllers and it's not possible to add each controller like this okay so if we go ahead and if we add hundreds or thousands of controllers like this then in the program.cs file we will have hundreds or thousands of lines of code just to add a controller as a service right so instead of adding each controller like this using this add transient what we can do is we can simply call add controller method and when we add our controllers using add controllers method asp.net core application will automatically detect all the controllers defined in the application using this controller suffix so using this controller suffix and it will add all of them as a service currently we have only one controller but if we have let's say 10 controllers in our application all those controllers will be added to asp.net core as a service by this add controllers method and in this way we do not need to add each controller one by one all right so here we have registered our controller now the second thing is we need to enable routing for the index action method so earlier in order to enable routing first we used to call use routing middleware so here let me go ahead and let me call this use routing middleware on this app object and then we also used use endpoints to Define our endpoints to this endpoint we can pass a middleware function this middleware function receives this endpoint as its argument and inside this endpoint we can simply map our controller for that we can say in point dot map controller this map controller it will pick all the controllers and action methods inside them and it will add routing for all those action methods to asp.net core application automatically and these are the two steps which we need to follow to enable controllers in sp.net core application now one more thing which we need to do is we need to tell for which URL we want to execute this index action method for that we are going to use attribute routing so we have talked about routings in great detail in our previous section one of the way in which we can define a route is by using attribute routing in attribute routing we use a set of square brackets in that we use this route and to this route we need to specify the route pattern so here let's say we want to execute this action method whenever a user types let's say home in the URL so after the root URL if the user says slash home we want to execute this action method and we want to return this text in the response now this route attribute here this is being used from microsoft.asp.net core dot components namespace but here we want to use the route attribute from microsoft.asp.netco dot MVC now you see the error is gone and with this let's go ahead and let's run our application and here in the URL after this root URL let's say slash home and if I press enter we should see this response welcome from asp.netco application so here when we are typing this route in the URL this index action method is getting called and this text content is returned in the response so this is how we Define a controller and an action method inside that and this is how we register the controllers now here when we are using this map controllers in order to use this map controllers we don't need to call it on this endpoint we can also go ahead and we can call it on app so we can say app Dot map controllers okay and when we use app.map controllers like this in that case using this use routing middleware and this use in point middleware is optional so basically when we use app.map controllers it will automatically call use routing and use endpoint behind the scenes so we don't need to specify that explicitly and if I go ahead and if I run this application again it should be still working so after this root URL let's go ahead and let's type slash home and if I press enter we have this response so in this lecture we created a simple controller called home controller and inside that controller we added one simple action method and for that action method we are defining a route here using this attribute routing so whenever the user types this route in the URL it is going to execute the section method this is all from this lecture in the next lecture let's learn more about controllers and action methods
Info
Channel: procademy
Views: 2,679
Rating: undefined out of 5
Keywords: controller, asp.net core, mvc, asp.net core tutorial, complete asp.net core mvc course
Id: Qa8q2LwcXE8
Channel Id: undefined
Length: 14min 23sec (863 seconds)
Published: Fri Mar 31 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.