How controllers work

Fast & Easy Programming
A free video tutorial from Fast & Easy Programming
About Fast & Easy Programming
4.2 instructor rating • 18 courses • 8,739 students

Learn more from the full course

Learning ASP.NET Core 2.0 MVC - Beginner to Developer

Learn ASP.NET Core 2.0 MVC 6 from the ground up, develop custom applications and use Entity Framework Core with LocalDB

03:18:55 of on-demand video • Updated March 2020

  • Learn ASP NET Core MVC from the ground up
  • Develop your own ASP NET Core MVC applications
  • Use Entity Framework Core and LocalDB
English [Auto] In this video explain how controllers work. So I have the product running here from the last video. And I said at the end of the last video that this home page here is the home controller it's in the exaction. So let's see what that is exactly in the product. I have this controls folder and the controller in there which again gets created automatically when you chose this specific type or template. When creating a new product and anyway if I open it I can see here the index section. So the methods instead of controllers are called actions. OK. So this is an action or inaction method and controllers always have the suffix controller and they extend controller class also controllers don't have to be in the controllers folder that can be placed anywhere. But they go into controls folder by convention which you should follow. OK. So once again all control of this controller suffix and the methods inside of controllers are called actions or actual methods. So how come this particular action is triggered when we go to the home page. That is because of routing. So if I go to the startup CEOs file that routing goes inside of this configure method and I hear this and then when I get to more interesting later on in it's own section but they have to mention this now. You can see here that there is a route. So this map route which goes controller slash some action slash some ID which is optional and the default for the controller and the action are home and index. And that's why this action is triggered when you go to the homepage because there's a default. But since routing works right now this way. So controller slash some action you can get to the same page by going to slash home slash index and you can see it's the same page. And also if you just go to flash home you go to this page as well because index is the default action you will get. And once again since both home controller and then the action of the defaults that's the rule. So this works as well. Also the type that it returns is can result. And so the actual result basically it can return everything web related so it can return abuse or raise send or redirects. You can also get more specific but usually this one is used because it can return anything related that is. But as I said you can be more specific. So I can say for example that the want the return of your your result that is actually. And that's going to work the same way so if I say no and the refresh and you can see that it still works this the more specific but as I said I actually resort usually used because it covers of use the Drazen redirects and so on. Anyway what is this return you do exactly when you return have you it may return and go of your values folder and if there is no you never specified here then expected expects you to be called the same as the action. So in this case it's looking for. Have you called in the case. And if I go into the folder and by the way by convention you should have your folder of the same name as the controller and then you have control or specific use inside of that folder like here. So here is the index and contact in about. And here you can see that there are those three method so in about contact. So if you open the X I can see here basically everything you see here except for the other which is coming from a different play. Apparently that does. But I'm going to get more into that later on. The point now is that if we just get rid of all this I can just say here index your like so save that and see this as well. And the refresh no is then the few so strength from actually the text from the view. And I also get this header and footer because it's still used in the parent layout. And once again I'm going to get more interviews later on. But there is this underscore layout which is used as the parent. So that's why you see the nav bar and the foot rule here. But I can say that I don't want to use alioth because if I don't say anything is going to use the default clock and to do that I can say here at. And this is for a razor so razor is is implemented. And again if there's going to be a section and again the razor is going to have its own section but then it to mention some razor stuff now. So I think curly braces and this is a razor block and I can say here lay out now. And now it's not going to use a layout so very fresh No. You can see that they just get in here draws the index template without anything else. And by the way you really you will return either of you or adjacent on a redirect but you can return anything. For example you can return a strength so I can say here just return type strength. And return I can say here here or there for example. And when I returned a string. OK. So if I refresh now and there it is. So this is just a shame. No it's not of you. It's a little strength. I don't see really a point to doing this but I just wanted to show that it's possible even though of course you're usually you're always going to have either of you Drazen or redirection returning. I imagine. Anyway that's it for this video. See the next.