From the course: Introducing ASP.NET Core

Making a simple API - ASP.NET Tutorial

From the course: Introducing ASP.NET Core

Making a simple API

- All right, we're about halfway through our asp.net 101 series. Feeling good. We've got a good looking website we think. It's got some CSS and some HTML. It looks somewhat attractive to the best of our ability. We've also got our cards. We hover over them. We're using bootstrap for a lot of this. This data is coming out of a JSON file but again it could come out of anywhere. In fact, this data could have come out of like a web service from someone else's web service that we would call. - Sure can. - Or someone could call us and we're a web service. - Yep. Or what if it's just we want to maybe take this site mobile but you know. - That's a really good point. We could make a mobile website or we could make a Xamarin web app and then make a web API that is going to provide these products as a service to someone else. Now, when you're doing that, you're not returning HTML you're returning JSON and your data could come from anywhere, with JSON as the format. So why don't we make a tiny, simple web API and then in the next video we'll make it even more interesting and more complicated. - Okay, so you used API. I don't like the abbreviations. - Yeah, so API, so Application Programming Interface. Right and that's the ability to go and call another thing in a formalized way. So everyone got together on the internet one day and decided that JavaScript was the way. And JSON, the JavaScript Object Notation is a good way to express things. So we have a list of products. And if you remember - It's kind like a service. - It is, that's a great point, it is a service. So if you remember, you had me making, where is it? There we go. You had me make this product. So that's the C-sharp class or the shape of our product. And we want to go and return that as JSON. - We do And we want to basically make it so you go to our website slash products and it returns as JSON. And remember at the very beginning of the first video there you had me do a JSON serializer. - [Leslie] Yes. - [Scott] That's going to come in useful in this one, isn't it? - [Leslie] I think it will. - [Scott] Let's find out. So the ability to build a serialized JSON or build a serialize, rather a C-sharp into JSON is super interesting. - You just write it back to JSON. - Now, right now, if I go to my app and I say, slash products nothing happens. - [Leslie] What was that about? Yeah. Well, it was a big fail. If I hit slash privacy, I get a privacy policy page. - [Leslie] That was good. - Yeah. And if I go over here, you can see I've got a privacy page. I've got my index, which is kind of my default. I don't have a products, but do I need a products page? Do I need a products endpoint you know, a place to land when you say slash - [Leslie] Well, apparently in the startup file that some of us tend to ignore such as myself. There, there isn't a spot where you can insert additional end points. - [Scott] Hmm there absolutely is, this end point of where a URL turns into some work is an end point in asp.net. And you can see here that by default, again this is a default. And you mentioned that you don't really look in startup.cs - No - Most people don't need to, cause you you get it configured. And then you spend most of your time making pages. We're going to do it in two steps. We're going to do an end point of our own that's custom. And then afterwards we'll do it another way. So here we see that there's an end point. And then we say MapRazorPages. It is because of that line, that slash privacy and and slash nothing actually works. We've also got an error. It looks like as well. - Yeah. - Okay. So turns out, you can say end points dot and look you can map all kinds of stuff. Map this, map Controllers, map delete, map fall. Oh my goodness. Well, you can map, oops am going to be typing a mapGet. so when someone does a get, we can do some stuff. - [Leslie] Awesome. So I noticed you were trying to use the forward slash products then so why don't we try to insert that as well? - [Scott] Okay, so that will become our new end point or end point route. If you've seen asp.net in the past you may be familiar with routing. You should go and take a look in the docs about the subtle differences between routing and end points. End points is a little bit more flexible for what we're doing. We're going to say slash products. Cool. All right, then this is a little tough. You say comma, and then you can have this object. I call it context. You can call it whatever you want and then our rocket ship. Right? And then we're going to go and open that up and then close it and close it. So let's just remind ourselves really briefly that got opened there. See how visual studio is so smart. - [Leslie] Yeah so nice. - [Scott] It is nice. We got the clause there. So our mustache is here, our curly braces there right? And are, there, so the rocket ship is only going to point to the stuff that we're going to do. - [Leslie] Yeah. - [Scott] We can do a bunch of stuff. So the stuff goes, - [Leslie] What are we going to do? Yeah. - [Scott] All right now this might look a little scary but you walked me through it before and I didn't feel too bad - [Leslie] Okay. All right. So in this case, I think we just want to retrieve all the products again. - Right, now because we're on a Page, we're going to have to go and get them manually. And this is actually no fun and proves that the way we did it before is actually cool because services should be easy to get. So we have our app and apps a thing, right? And we can ask all of our apps, application services. - [Leslie] Go retrieve the specific service that we want. - [Scott] Gimme, which was called a? - [Leslie] JSON file product service. - [Scott] JSON file product service, cool. So it's already getting too long and it's stressing me out. - [Leslie] Yeah. - [Scott] Okay. Trust me. This will be worth it because we are teaching - [Leslie] There's lots of function calls here too. - [Scott] Teach a woman to fish. - [Leslie] Yep. - [Scott] Now get products, - [Leslie] Which was a function that existed right when we made it. - [Scott] So all at one like long breath line. You're going to go and say, app.ApplicationServices go and ask the app, give me this service, that GetProduct, so I could have just made one. But in this thing here, I'm actually saying, you know about these, just give me that. It'll be easier. Like life will be better if you go and make this happen for me. All right. So then on the next line we get to the actual JSON. - [Leslie] Hmm, so this case, we got the JSON variable and we need to serialize all those product objects. So we're going to use that JSON serializer again, - [Scott] JSON serializer may not be available here and I'm going to use that little light bulb and get that using statement added at the top. Remember, all the way at the top. It just added that, Okay. JsonSerializer.Serialize. Okay. So not deserialize but serialize and we knew we can make a list of products or whatever products it's going to be an iEnumerable of product. Another thing that we need to bring in from models. And I'm just hitting control dot, to get that up very quickly. And then we're going to say, these are the products. These ones here, make me a string of those. - [Leslie] Yep. - All right now, it is worth reminding ourselves and the audience that we're doing this inside of startup.JSON, which is weird. - Yeah, - it is weird but it's also showing you that there's a low level or like a down on the metal way that you can do things. And in the next video we're going to do this in a much, a much better way I think, so far we have retrieved our product by manually digging it out of the application services pile of services. And then we serialize the product into some JSON let's go ahead and return that to the web, web browser cool? - [Leslie] Good idea. - [Scott] All right so back to visual studio. - [Leslie] Alright, So now let's go ahead and return, so we're going to use return keyword and then context.response. - [Scott] Right, We have response and requests. So we're going to go back to response. Okay. - [Leslie] And we're going to do write. - [Scott] I'm sorry. I don't see, write? - [Leslie] Yeah. That's interesting. Let's find it anyway. And maybe we'll get the trustee. - [Scott] Well, there we go. So there's our writeAsync is inside of a .HTTP namespace when I add that - [Leslie] light bulbs to the rescue. - [Scott] Yeah. The light bulb is great. Oh look and here we've got a different choices. We're going to return some texts. So I'm just going to say JSON and add a semi-colon and that JSON of course, is this one right here. - [Leslie] Yep. - [Scott] All right. So then let's just go ahead and run this and see if it works. Just sound like a plan? - [Leslie] Yeah. - [Scott] Deal. - [Leslie] Fingers crossed. - [Scott] It's kind of low confidence. - [Leslie] You never know. - [Scott] It could work. Okay. So there's our webpage. We knew that that worked but remember we made a slash products, products. - [Leslie] Oh. - [Scott] There you go. - [Leslie] Nice. Normally on my browser though, I don't see it that nicely formatted - [Scott] Yeah if you notice. I'm going to hit refresh like watch again, really close. It looks bad. - [Leslie] Pause your, pause the frame. - [Scott] Yeah. It looks bad for a second. - [Leslie] Yeah. - [Scott] Well it turns out what I'm doing, I'm using the new Microsoft edge and it is based on chromium. And you can use JSON view, which is a it's literally pretty JSON. - [Leslie] That's pretty. - [Scott] All right. So I recommend that people try that out if you want to look at JavaScript because if you look at the reality, if I do a view source, we don't need it. We don't need to pause the frame. That's the reality. But of course that's not for humans. - [Leslie] No, I wish you can tell its most likely successful. - [Scott] That's totally okay, that's not for humans that's for, for, for for the future mobile website or the mobile application that you mentioned. - Sure. - Cool. Now, again, not super fun. - [Leslie] No - [Scott] let's take a break, come back and make a real web API. That's much more friendly. Deal? - Deal. - Stick with us.

Contents