(#13) Controller in Asp.Net Core MVC | Asp.Net Core tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome again to asp.net core mvc tutorial in this video we will learn the first main pillar of mvc architecture which is controller so we will learn all the details of controller in this video in this video we will start from what is controller and we will see what is the role book controller how can we add a new controller in your asp.net code application what are the action methods how can we call a controller the cooling of controller means how can we call the control by using http request how can we pass the parameters in xn methods once we will understand about the action method then we will learn how can we pass the parameters in action methods this is a major confusion in between the developers when can we create a new controller and at the last we will see how many controllers can we have in one application should we have only one controller or should we have more than one controller and what maximum number of controllers can we have into one application so let's start what is controller controller is a special class controller is a special class with dot cs file extension if we are using csr for the development of php.net core application then the extension for control class will be cs it means you will write the c sharp code into the controller class by default controllers reside in controllers folder when you create a new application by using msc template then you will see there is a folder which is called as controllers and inside that controllers folder you will find all the controller which we have into the application in mvc template controller class is inherited from controller class remember we are not talking about the web api we are talking about the mvc only so if you are using mvc template where we are using add mvc or add controllers with views then the controller class will be inherited from controller class now let's understand what is the role of a controller here on the left side i have a client you can call it a browser or any other client that you are using and here on the right side i have a server and on this server an mvc application is working these three boxes you can refer to model view and controller this one you can refer to view this one controller and this one has a model when you send a request from client to the server then first it goes to the http pipeline once the request will pass from this http pipeline then it will hit the controller then inside the controller there are lots of methods the request will go to a particular method and then the logic of the application flow will be written over there into that method then that particular method will generate the response and it will send back to the browser so this is how the general flow of asp.net code msc application works now let's learn the role of controller in detail a controller is used to group actions all the similar actions or operations we define in one class what are the actions what are the operations suppose i am working on an employee application so getting the employee is one action adding a new employee is one action getting the home page of the application is one example getting the about facebook application is one action so these are some examples of the actions and we generally group the similar actions into one controller controller is used to handle an incoming http request as i have already told you into the previous slide that when we get a request from client to server then it goes to the controller you cannot hit a model or view directly we can only hit a controller the mapping of http request is done using routing at this point i told you two things that we can have multiple methods inside one controller and we can call only these xmeters from the browser or client the mapping of request to the method of controller is done with the help of routing so routing is the important feature in asp.net core framework and by using the routing feature we can map the request to a particular method of controller controllers can also be used to implement several type of other features just like caching and security security meaning authentication and authorization there are lot of other features also which you can implement on the controllers all these features we will learn into this tutorial now let's learn how can we create a new controller into the application here i am on the visual studio and here i am using the same application that we have created for the bookstore application in the previous video i created this new controllers folder and inside this controllers folder i have created one controller which is home controller now let's add one more controller to this application so to add a new controller you can right click on this controller choose add and click on this controller option once you click on this controller option you will get several options so three options are related to mvc and three options are related to web api so if you are going to create an mvc controller then you have to choose any one of these or if you are going to create a controller for apis then you can choose any one of these template since we are working with the mvc and we will start from this empty controller so i can use this empty controller second one we have mbc controller with read write actions actions mean public methods if you will use the second template then few methods will be created for us automatically third one is missing controller with views using entity framework so at this point we do not have any knowledge about the views and attitude framework but just to tell you that if we are using this third template then by using the entity framework automatically all the views all the classes will be generated for us and we will have a working application but since this is the beginning of this tutorial so we will add an empty controller for the learning purpose okay let's choose this empty one and click on the add button remember guys every controller class must have controller as a suffix that is why over here in this window you can see only the default is highlighted if you will make changes then the text of default will be replaced to the new name but you do not have to change the controller suppose i am writing over here suppose i am writing over here book controller so basically i am going to create a new book controller okay you do not have to remove this controller class let's click on the add button now let's see the basic detail of this controller class here you can see the extension of this file is dot cs the name of the class is book controller and it is inherited from controller class this controller class is available inside microsoft dot asp.net core dot msc name space let's right click on this one and go to the definition here you can see we have some methods related to the development of application we have temp data view bag view data on action executed executing partial views views view component so inside this controller class we have several methods and if you will focus on these methods then these methods are required when we are working with the mvc template it means we are using views into the application there is one more class controller base that's the details of this controller bs class as well now let's click on the go to definition here you can find the details of the http request the response route data model state model binder and basically all other things which are required in every application that you can find it over here so in general we can say when we create a new api controller then we do not need the views so at that time we will not be using controller as a base class we will be using this controller base class and for the mvc controllers we will be using this controller class as a parent for our controller we do not have to remove the suffix controller from the name of our custom controller so this is how you can add a new controller into the application now let's see what are the action methods in a controller all public methods of a controller class are known as xn method because they are created for a specific action in the application it means in one controller we can have several action methods and each xn method is created for a particular operation an excel method can return several types of data just for the learning purpose let's remove this method so that we can create our custom methods by ourself at this point i assume that we know a control class is used to group similar type of actions and since we are talking about the book controller class it means in this controller class we will define all the actions related to a book what are the actions related to a book for example i need to get the list of all the books that we have into the application so getting all books is an action adding a new book is an accent deleting a book is an accent editing a book or updating a book is an action so here in this book contour you will define all those methods for x and methods which are required to perform several operations on book so let's start one by one public suppose the return type is string remember guys i am creating a very dummy method inside this controller class in several upcoming videos we will modify these methods according to the real one but for now just for the learning purpose i will start everything from very basic so here you can see we have a very basic action method which is that all books so this method is used to return all the books that we have into our application and since we are using string as electron type so we can return so at this point we can see that we have created one action method inside our controller class now let's see how can we call an xn method into the controller class when we get a http call on controller we actually are hitting this request on a particular action method whenever we say that we are hitting and controller it means we are hitting an accent method of that controller we never hit only a controller in all the scenarios we will hit a particular xa method of a controller inside one controller we will have multiple action methods and whenever we will get a request from the client then ultimately we will be hitting one of these action methods which we have defined into the controller class and here is the default structure to code a particular action method first we will have the domain of the application since we are working on the local development in our scenario the domain will be localhost then the port and then the name of the controller without controller suffix and then the name of the action method so this is the basic structure to call a particular action method so let's go back to the visual studio and here we have one controller which is book and we have one action method get all books so it means if i call domain slash book slash get all books then you must get this output into the browser let's verify it by pressing ctrl f5 so this message is coming from the default route which is home index in the home index we have defined web gentle that is why we are getting this message if we need to navigate to this get all books then we can write book and we can type get whole books press enter here you can see we got the message all books and this whole books is coming from this get old books method here is one more point to notice that i am using the name of the controller and action method in small characters remember guys if you are writing a url into the browser then the capital or small letter doesn't matter you can type any one of them you can write all of them into the capital or all of them into the small just for the testing if i write this s into capital and press enter again i will get all books into the output so suppose i'm again just for the testing this o i'm typing in capital and press enter then again you will see we are getting the same message so the small character or capital character doesn't matter into the url we simply have to type the exact name and that's it now let's understand how can we pass the parameters inside this action methods and for that i will create one more action method which is public stream get book suppose i want to get the details of one single book then return book okay and to get a single book from a collection of books we need some identification so that we can figure out which book we need to get from that collection and to identify that book or to filter that book we need some parameter suppose i need an id suppose i have five books in the database one two three four five and i need the details of a book with id four or three or two so i cannot create multiple action methods for each id we can simply create xn method as a generic action method and here we can pass the id so this xm method will work for all the ids that we will have into the database it will work for one two three four and these one two three four the details of id we will get from the client how let's learn how to understand it into the better way first let's open this startup class inside this startup class we have already used this method which is snap default controller route if i right click on this one and go to the definition let's open the details of this method also here is the pattern that we have defined for the default route first we have to pass controller if you are not passing the name of controller then by default it will be home then after the name of controller we have to pass the name of excel method if we are not passing the name of action method then by default it will be index so suppose inside my book controller i have one exa method which is called index and into the url i am only typing book so by default it will hit the index method of the book controller why because index is written over here if you will not define the name then by default these will be the names and as a third parameter i can pass the id if i pass the third parameter into the url then it will be id if i do not pass then there will be no error now let's go back to the controller here you can see we have the book controller we have the xm method book and now this time we will be passing the id let's learn how and now let's return this id also now let's press ctrl f5 so remember the default url of the application is home index every time you will run your application it will hit the index xm method of home controller until you will define a new url here in this browser to get the details of all books we can simply type book and get all books so using this we can get the details of all books and now we have to get the details of one single book so first we have to define the name of controller with this book and second is get book so this is the name of action method then slash m id so suppose one and if i press enter over here then you can see we are getting output book will id equal to one if i type two over here and press enter then you will see i'm getting the details so book two don't focus on the details just focus on the concept so we are creating only one excel method for all the books to get the detail of all the books we will be using with this single action method we do not have to create an excel method for each book we can simply fetch the data of all books by using only one accent method this get book action method is used to get the detail of a particular book and we can use this getbook method only in one scenario where we know the exact id of that particular book but suppose if i want to search the book for that let's make a new action method so public string don't focus on the return type of these methods we will learn all these things in upcoming videos for now just focus on the concepts about the xn method and controller search suppose i want to search the book based on the name book name also i can search this book based on the author name okay let's return the data now in this scenario we have two parameters first one is book name second one is author name and since we have defined only id into the default route then how can we pass multiple parameters into this action method for that let's run the application here is the default route now we have to go to the search books action method and first you have to type the name of the controller which is book then we have to type search books and now we have to type the book name and the author name how can we do that and if you do not have id then we will use query string to pass the data from this url to this action method and how can we use the query string first you have to use question mark then you have to type the name of your query in our scenario the first query or first parameter is book name so we will write book name equal to define the value we have to use equal sign and then the name of the book suppose i am writing over here mvc book so this is how we can define one parameter and you have to use question mark only for the first time if you need to define one more parameter then you can use ampersand and then you have to define the name of your second parameter which is author name then you have to type the name of your author which is suppose it is with this okay and let's press enter here you can see we got the output okay oh so there is a type of mistake boom that is why we are getting this work over here with name let's focus on the output the name of the book is mbc book and author is this let's put the breakpoint over here let's refresh this page so you can see we got the request here in this search box and inside the book name we have the name of the book which is amazing book and in the second parameter author name we this is how we can get the details of both the parameters so first parameter we have book name then the author name and we can modify the output as per the need of the application here just for the learning purpose i am writing back to the output so that we can identify the exact output but in the general scenario based on these parameters we will get the data from the database or some other resource and we will display it on the ui let's continue if you will change the name of the book suppose instead of using mbc book i'm typing here java and press enter then in this then in this case you can see the name of the book is java and the author name is with this let's press continue and here you can see we go to the output java book and author is this so this is how you can pass the parameters to your action method then simply create a new controller whenever we need to define a new group of operations into application so suppose earlier we were having home controller into this application and for the home controller we can define several actions just like the home page the about page contact page all the basic details about this application we can write into home controller but now we have to develop something related to the book so all the operations related to book we will define into book control let's take one more example suppose i need to implement the security login logo king password forward password all these things for all those related operations we can create one account controller or membership controller you can give any meaningful name and we can create all those action methods into that new controller ultimately the result is whenever we need to define new group of operations or we can say whenever we need to define similar operations then we can create one new controller now the question is how many controllers can we have into one application depends on the application if you are developing a large scale application then that application might have a number of controllers at least one controller is required to perform operations since all the http request comes to controller action method it means we need at least one controller inside the application to work in the mic pattern maximum any number of controls we can have into one application so basically it all depends on the requirement of your application if you have to work on loads of operations into your application then you can create a number of controllers you can define all the operations into one controller as well but that might not be a good architecture to create a good architecture for your application you have to divide your application into small groups and for each small group you can create one controller but so never ever use only one controller inside your application based on the requirement or operations or group book operations you have to create or you must create a new controller that is all in this video i hope after watching this video you will be able to understand the controller into detail in the next video we will understand about the models into asp.net core till then thank you for watching have a great day
Info
Channel: WebGentle
Views: 35,492
Rating: undefined out of 5
Keywords: Controller in Asp.Net Core MVC, controller in asp.net core, what is controller in asp.net core, add new controller in asp.net core, add a controller in asp.net core, call controller in asp.net core, pass parameter in controller in asp.net core, query string parameter in controller in asp.net core, asp.net core mvc, asp.net core, .net core, asp.net core mvc - complete application, asp.net core mvc project, asp.net core tutorial, asp.net core mvc tutorial, webgentle
Id: KxIMpq2btKs
Channel Id: undefined
Length: 22min 50sec (1370 seconds)
Published: Sun Mar 08 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.