Watch videos with subtitles in your language, upload your videos, create your own subtitles! Click here to learn more on "how to Dotsub"

ASP.NET Web API, Part 4: Paging and Querying

0 (0 Likes / 0 Dislikes)
  • Embed Video

  • Embed normal player Copy to Clipboard
  • Embed a smaller player Copy to Clipboard
  • Advanced Embedding Options
  • Embed Video With Transcription

  • Embed with transcription beside video Copy to Clipboard
  • Embed with transcription below video Copy to Clipboard
  • Embed transcript

  • Embed transcript in:
    Copy to Clipboard
  • Invite a user to Dotsub
[Microsoft] [] Hi, this is John Galloway with Microsoft. We're continuing our look at Web API with a look at how it handles some more advanced querying scenarios. In a previous screencast, we looked at two very simple query cases. Get all comments and get a comment by ID. Now we'll extend on that, starting with manually paging using query string parameters. So you'll see here our GetComments method takes in two parameters, pageIndex and pageSize, and we're able to call any logic we'd like here. So in this case, we're calling into our repository and pagings and skipping manually. We can always drop to this level, which is nice. Inside our JavaScript, you'll see, we're just concatenating to build out the URL and calling into that method. So you'll always have the ability to do that. However, if you would like, with Web API, you also can take advantage of OData query syntax. So in order to use that, all we need to do is change our GetComments method from returning IEnumerable to returning IQueryable. And you'll see here we're just using a simple extension method to convert our IEnumerable to IQueryable. In our JavaScript, we still need to build out that query URL. So you'll see here we're using a top and a skip parameter, and then we're passing those in. However, on the server side, Web API takes care of handling all that, all those query parameters, and just doing that for us automatically. And all we need to do get that support is return an IQueryable. So you'll see here we've got a very simple page. We're able to search either by Query String or Queryable. I can search, and either way I get the same results back. I'm able to set a size, page size, and here I've got two. IQuery. Queryable, same thing. So let's take a look at the developer tools and see how that works. So I'm starting to capture, and you'll see when I search by Query String, I've got that pageSize and pageIndex query string parameter. When I search as Queryable, same thing, except here's I'm using top and skip. So a few important things to point out with this OData query support. First of all, we are only using OData query syntax, so you'll see here that our OData query did not require us to return any special OData syntax, just a standard JSON. All we're doing is making use of the OData query syntax. So a second thing that's nice about this is that I'm able to take advantage of other queries very easily. So you'll see here I'm also ordering by author. So I'm just enabling that in my JavaScript and made no changes on the server, and now when I search as Queryable, I'm going to get these back alphabetically sorted by author. So you'll see that that's very nice to be able to take advantage of more advanced OData query support, without needing to make any more changes on my server other than returning as queryable. So that wraps up our look at advanced querying in Web API, starting with manual implementations, which you're always able to use. And if you'd like to use OData query syntax, it opens up a lot of powerful options for you. In our next screencast, we'll be taking a look at validation in Web API. [Microsoft] []

Video Details

Duration: 3 minutes and 33 seconds
Country: United States
Language: English
License: All rights reserved
Genre: None
Views: 6
Posted by: neudesicasp on Jun 24, 2013

OAuth in the Default ASP.NET 4.5 Templates

Caption and Translate

    Sign In/Register for Dotsub to translate this video.