ASP.NET Web API, Part 2: Getting Data
0 (0 Likes / 0 Dislikes)
[Microsoft ASP.NET]
Hi, this is John Galloway with Microsoft.
We're continuing our look at the ASP.NET Web API.
In the first screen cast, we just took a look at the file new project experience.
Now let's actually build out a simple application.
In this case, we're going to be building a comments application
that's going to allow for getting, updating, etc., comments from a client via AJAX.
And so to do that, first of all, I've renamed my controller
to a comment controller, and I've created a very simple model class.
This is very similar to what you would expect in an ASP.NET MVC application.
So I've got an integer string and 2 text properties.
So now I can go into my controller and I want to change
my controller so that it's returning a comment,
and so that's returning a numerable of comments,
and when I request a specific by ID, I'm going to return comment.
Now similar to ASP.NET MVC, it's a good practice not to put
a bunch of data access and persistence logic in your controller.
You want to abstract that somewhere, so in my case,
I'm going to be using a simple repository.
So to keep things simple, I'm just storing things
in memory here in a simple dictionary.
Of course, this could be using an entity framework code first,
or whatever you'd like to use to persist your data.
So now, I can go back into my comments controller,
and I can start wiring this up.
So of course, the very simplest approach is just new upper repository
and use it to return things, so I can say var repository
equals new dictionary repository, and then I can return things directly from that.
So now let's take a look at that get by ID case,
and I'm going to keep with a naive implementation.
Don't worry, we're going to clean this up in a second.
So I'm just going to create a new repository,
and then we're going to go get a comment.
Now this is a little bit different here, because if the comment
doesn't exist, we need to do something with that.
So you can see I'm calling into the repository.
If I get a comment back, that's great, I'll return it.
If I don't, I'm actually going to throw an except.
So the correct thing to do here is throw an HTTP response exception,
because I shouldn't just be returning an okay, sorry there's no information.
Since we're embracing HTTP here, the correct response when
you request something and it's not there is return an HTTP 404 Not Found.
Now this is kind of silly to be newing up a repository in
each controller action, and a better approach would be to use
a service locator pattern that's already built into ASP.NET MVC.
So that's exactly what I'm going to do.
My controller now takes an iComment repository,
and the service locator's going to wire that up for me.
Let's take a real quick look at the UI lair.
So my client's just static in this case.
I'm using jQuery to do my requests back to the server
and I'm using KnockOut to render it, but it's really pretty simple.
And I'm using a get JS script when I click on a button
that's going to call back and get some information.
So here's what that jQuery looks like in the get.js function.
It's literally just going to respond to a button click,
it's going to call back, so it's going to do an HTTP get
back to that API comments, which was exactly what
we just created in that controller, API values controller,
and it's going to render that information.
So it's going to call into /api/comments, which is going to
call into comments controller, which is going to do a get.
Let's run it.
So I'm going to hit F12 in Internet Explore to bring up my developer tools,
and I'm going to capture network traffic so we can see what happens.
So I click on the get button, you can see it's made a request
back to the server, and the response body shows what
the comments controller returned to it.
So here we have a very simple request to the server,
it's gotten comments, and it's rendered them on the screen
while we've been looking at this, and here they are.
So we've covered the simple get scenarios, and in the next
screen cast we'll take a look at the create, update, and delete.
[Microsoft API.NET]