From the course: Introducing ASP.NET Core

Enhancing your web API, part 1 - ASP.NET Tutorial

From the course: Introducing ASP.NET Core

Enhancing your web API, part 1

- Hey friends, we are continuing our ASP.NET Core 101 video series. In the last video, we made a very, very simple web API that was really kind of showing how one could make a web API, but I'm not feeling too proud of it. - Yeah, it's not the most ideal way to go about it. - Yeah, it's not the most ideal way, but it showed us a bit of plumbing, a bit of the internals of ASP.NET and we ended up doing it in basically three lines in startup. Leslie's kind of feeling gross about it, I'm kind of feeling gross about it. And it's not bad. We're teasing each other a little bit here but the point is that's if you do it manually. Let's not do it that way shall we? - Yeah. - All right. - Why don't we separate all this stuff out into its own separate spot. - Right, because we've been talking about the need for a single responsibility principle or what they call, that separation of concerns? - Yeah. - And then we did this horrible thing. - Yeah. (laughs) We've got to practice what we preach. - Yeah, so I comment that out. I'm going to say Control + K, Control + C. Look at that. - Awesome. - Comment that out, so we no longer have a web API. - Mm hmm. Earlier you made me make a Models folder and that's where we're currently keeping things. And Pages are kind of views. So we've got models and views. - [Leslie] Yeah, so I guess all that's missing is the controller. - Right. - [Lisa] For those of you familiar with MVC or Model View Controller. - Right, ASP.NET is a very flexible web framework. You can make APIs, you can make Model View Controller type things, you can make just Pages within Page Model and you can mix and match them all. It's not... It's not really prescriptive. You can do what makes you happy. - Mm hmm. - All right, let's do that. So I'll just make a folder if that's okay. - [Lisa] Yep. - [Scott] Add a new folder. Controllers, is that cool? - Mm hmm. - All right. - [Leslie] And then we can call the new file product controllers. - Okay, so I'm going to say-- - [Leslie] Or ProductsController. - Controller products, plural. - Mm hmm. Okay, ah, this is interesting. So when I said add controller, I could have said add class. - Yeah. - But I said add controller and they're giving me a little bit more, a little bit more feedback here. I could make an empty API controller. I think that'd probably work for our needs. - [Leslie] Yeah, that sounds about right. Since we're enhancing our API. - [Scott] Okay. So we'll say, add and then we'll say ProductsController, plural. That's going to go and scaffold that out. It's going to kind of create that. Just take a moment. - Mm hmm. - [Scott] And when we create this we're going to have the products model and the ProductsController. - So why even have MVC? Like what's the point? - That is a good question. And the idea is that in separation of concerns you want to have the model that knows how to talk to the database. You want to have the controller that is your your orchestrator that has left-hand in the database and right hand in the views. And then your views and views know nothing about anything. They only know about how to display information, display data. - And again that's great, 'cause it's like the whole ignorance is bliss for code. If you want to reuse this stuff or expand on it later it's a lot easier to do that if you don't have so many dependencies between. - You know, you said that before that idea of ignorance is bliss and let's make sure that that's a joke that people get, because it really makes sense though. If your object knows nothing about it's, about anything other than it's one job, it's a much more useful object. - Yeah, absolutely. - And if your object knows all about the internet and then I don't know the internet goes away, who knows what happened? - I don't know. But the problem is if it knows everything, you end up with these giant monolith classes that you've probably seen before. You've probably made one before if you're not new to programming itself. And then you kind of regret it later when you try to find or do anything else with that later. - [Scott] Exactly. So we just created a ProductsController and ProductsController has this controller base that it derives from, it inherits behaviors from ControllerBase. So it can do stuff that ControllerBase can do. - [Leslie] Again, that was nice in Visual Studio. It did all this for us so we didn't have to figure out what we needed to be extending. - [Scott] Right, when we made a new controller, we got this for free. - Yeah. - [Scott] It is an API controller and it meets a certain route and we will figure out how to plug these things in. But right now it doesn't really do anything. We could've made one that did more stuff, but that's fine. - [Leslie] Yeah. So speaking of making it do stuff-- - Mm hmm. - Maybe we should add-- - Make it do some stuff. - Yeah. - [Scott] Now in this case here, they've got us putting it at API slash controller. This is products. I kind of want it to just stay at products. So if it's good with you-- - Fair enough. - [Scott] I will delete that. 'Cause it could be food slash bar slash products. - [Leslie] Yeah, you can create whatever crazy route you want. - [Scott] Yep and I want our route to be like super basic. Right, if that's okay? - Yep. - [Scott] And then we're going to make a constructor. Cause it needs to construct things, right? - Yes. - So what do we call it here? ProductsController. And I'll just make a regular default one. - [Leslie] Mm hmm. - [Scott] But, hmm. - [Leslie] you know that service that we made a while ago it was like JSON file. - [Scott] Yup, JSON file. JsonFileProductService. That's where our products come from. We're going to need one of those. - [Leslie] Yeah, maybe you should take advantage of that. - [Scott] Right, so we could new one. We could make one in the, in the old days. But no we're using the services technique that is available to us in ASP.NET. So we don't actually new them. We don't go var j equals new. Instead we just ask for them. And we ask for them by adding them to our constructor. And we can ask at, remember the logging? - We can add logging. - Yes. - [Scott] If you take a look at one of our previous videos we had logging. This controller could have that as well. - That's great. - That's cool. And then we need to hold it somewhere. Have it lying around somewhere. So we'll go and say JsonFileProductService, ProductService and then we need to be able to get that, you know, whenever. - [Leslie] Yeah, we don't need to modify it right now either. - Okay. And then we'll say this ProductService equals that ProductService. - Yeah. - So when... Who makes controllers, right? I mean, we don't. ASP.NET just makes 'em. - Yeah. - There's a controller factory in fact. - Ooh. - Yeah. The controller factory, every time you make a call, the controller factory says you got any product controllers? Oh, what does a product controller need? It needs these services. It needs logging. It needs JsonFileProductService. Wires it all up and says here, made it for you. - Going back to design patterns days. - It is. Yes, it's exactly what it is. It is a nice design pattern. That is our basic thing here. Before when we did a page, you and I were doing Razor Pages. In the code behind, we said on get. And there's a similar kind of a model if you're going to do things with controllers, isn't there? Yeah, I believe it has to do with like HTTP. So you have HTTP posts or get. Let's use get in this case. - Yep. That's exactly right. So we're going to make a method that returns product. Again, I'm going to hit control dot. That control dot pops up that menu. And we're going to do get. And to your point, it will be an HTTP. Look at that. - Awesome. - There's also a list of all the firms. - So many options. - [Scott] Right, so I can pick some of the more obscure verbs. We'll use some of the more obscure ones a little bit later. When people get stuff, call get cool. Notice how it's kind of threatening me right now. Hey, not all paths return a value. - [Leslie] Not returning anything. - [Scott] In fact, nothing's happening, right? All right well, where do products live? In the service. - In that service, yeah. - [Scott] And return. - ProductService. - Okay. - Dot GetProducts. - Dot GetProducts. - [Scott] Cool. Let's compare these for a second. So this is a nice clean controller with a lot of flexibility. I can change the route. It doesn't really know anything about anything. It just does one job. It is a broker. - That's pretty great. - Mm hmm. Okay, let's compare that to what we did before. (Scott yells) - [Leslie] Ugh, so much nicer. Notice all those bagillion function calls happening in just that first line alone. - Mm hmm. - It's really messy and not... - There's a lot going on here. We're digging around. We went digging for our service and we went manually serializing. This is a great point here. This line here, where we called the JSON serializer ourselves. - [Leslie] Mm hmm. - We didn't mention JSON. - It's done. Yeah, 'cause we already put it in that ProductService. - [Scott] Let's see if it works though. Here we are complimenting each other. - Yeah. (laughs) - And we don't know, what if it doesn't work? - Yep. - [Scott] Somehow that would be bad. Let's give it a try. So we'll run it again. - And we're all talk. - [Scott] Yeah, we are all talk, who knows. Products. - Oh. - Well, I have a thought. I bet you it's capital P, no. Interesting, let's go find out what's going on. Did we tell ASP.NET that controllers are a thing even? - You know I don't think we did. Maybe we shouldn't neglect that startup file after all. - That's what you get. - Yep. - [Scott] When we're talking smack trying to compliment each other. - Yeah. Talk a good game. - [Scott] Well, we mentioned before that our application uses routing and uses endpoints. We added that JsonFileProductService. But yeah, we didn't tell it about controllers. - [Leslie] Yeah, sorry start-up. So let's tell 'em. So let's go back down to, or we already commented that stuff out. So why don't we add a controller service under the configure service method? - So services.add. - Mm hmm. - [Scott] And we can add the entire MVC, like everything views and controllers and like the whole kind of thing. But instead let's just go and add controllers. You only want to add this stuff you're going to use. - Yeah. - Maybe later we'll add other stuff. But for now-- - Keep it minimal. - [Scott] Keep it minimal, exactly. So this is a very common pattern. You add stuff and then you use stuff. - Yeah. - Okay. - [Leslie] Yeah, so back under the end points land we're going to add another end point. We're going to map to our controller. - [Scott] So we're going to say MapControllers. Ah, interesting. So we've got, remember how we talked about this Razor Pages slash privacy slash error? Controllers, you can't see it, but it will be slash products because we routed it as slash products. 'Cause that's a name of our controller. And again, worth pointing out, we could always say slash foo or whatever if we feel like it. - Yeah. Now in this case we've been getting JSON this whole time with this API that we made. But like, what if we didn't want JSON. Could we swap to like XML? - That is a very good point. So right now it's assuming that I'm just going to return a list of products but it doesn't say as what. Do we want the ProductsController to be responsible for that? Maybe we would want to configure that, 'cause it's, you have to ask yourself, is that a business problem or is that more of a check a box maybe problem. You can, if you want go check out the docs and learn about how to add XML serializers to this and teach it how to automatically return XML. And the cool part is no code would change in the controller. - That is excellent. - Isn't that fun? All right, now, do we think it will work? - I think it'll work. - Pressure. - Feel good about this. - Pressure's on. I don't know. Okay, I just hit Control + F5. I'm going to go ahead and run that. - [Leslie] Right. - [Scott] I'm going to hit slash products. Yay, there we go. - Yeah. - Now we're back in business. TCB. - Yep. - Fantastic. - I don't know what that stands for. - Taking care of business. - Oh. (laughs) Fail. - We're TCBing here doing an ASP.NET core. Let's take a break and we'll come back and we'll extend that service with not just to get, but with something else. Cool, TTYL.

Contents