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

Services and ASP.NET

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, I'm Scott Hanselman, and I work on Azure, ASP.NET, and Web Tools at Microsoft. And I want to talk today about services and ASP.NET. We've talked a little bit in previous videos about web forms and web pages and making websites. We've talked about tooling and what that means in Visual Studio. I've talked about SignalR and real time. I want to talk specifically today about web APIs and this idea of an API service or a REST-based service. Now over here in Visual Studio, I've got my little Person application that I've made before. I'll just go and hit /person, and I've got a list here with one person. I'll hit Create New. I'll put in, let's say, Scott Gu. Make him super old. And then hit Insert. So now I've got two people. It's pretty straightforward. This is something that I scaffolded out in the web forms and websites video. The person is pretty straightforward and looks like this, with an ID and a first name and a last name. But I might want to get to that person in a way that isn't necessarily the website. I might have a Windows Phone app or a Windows 8 app or an iPhone app that's going to talk to this back end. I want to be able to access that same data in a RESTful way, as a resource, perhaps as XML or possibly as JSON. So what I can do is I can create a Web API controller. This is part of We've got a Person controller that derives from API controller, and this is really important. This is going to use the exact same database context, that exact same service that the website is using. This means if you're getting your data from a mainframe or AS/400 or MV Framework or NHibernate or anything like this, that data layer can be reused, certainly, right? That data layer can then feed your website, or it can feed your Web API, and then Web API can itself go and feed applications. So here we've got a GetPerson method. Now this is using a convention. The fact that it has get in the name of the method is significant. By visiting api/Person, it's automatically going to call this GetPerson. And it's going to return an innumerable list of persons or people. This is going to get it out of the database, and notice that we're not saying return this as JSON or return this is as XML. In fact, Web API is going to do the right thing, based on what the HTTP request that comes from the client says. So I can present that in a couple of different ways. Let's do this. Let's do it at the command line, just to make the point. So we're on localhost at this port, so I'm just going to use cURL, the command-line utility, and say, let's go and request localhost 11121/api/person. And that's going to give me those two people. Okay? Let's add another. You can't have too many Scotts. Let's add Scott Hunter. Make him very young. There we go. And I'll make that cURL call again. Now I've got Guthrie, Hanselman, and Hunter. You'll notice that's coming back as JSON. Now I could also say /4, using the ID of the newly created Scott Hunter, and get back just that. So I'm basically navigating my data, like it was a resource. Now I want to get this data in different ways. Let's go into a little bit more sophisticated tool. We'll use Fiddler as a great free tool for being able to explore your services like this, and I'll say /api/person, and hit Execute. And that has come back as JSON. Here's the raw result. But I can actually compose this. I could say, well, I want to accept not JSON. I want to accept XML instead. So I'm not changing the URL, but rather telling it, via HTTP headers, that I prefer XML. You'll notice that the body of that, we can immediately tell, is larger. We'll click on that and see, here's the raw XML. And there you go. This is the XML presented as a tree. There's our XML. Now I could even change this. I could use Web API and say, well—say that I only want PNGs. Maybe I'll return a picture of that person. Or perhaps I only want to have PDFs. Maybe I'll return that person's resume or the information about that person as a PDF. It's totally up to you. How you want your web service to look is up to you. Now let's look at a bit more sophisticated example here. This is a single-page application that represents a to-do list. Now this to-do list is a single-page app, meaning that everything is all on one page, and all calls are using JavaScript. Let me go and sign up here. Oops. Log in, then. There we go. So here's my to-do list. But you'll notice that I've never actually left the page. See the URL's not changing there? I'm adding lists. Buy milk. House things. Call Mom. This is, in fact, making calls to the back end. I'm going to hit F12 and bring up the F12 tools here. And pop that off to the side. Okay? Let's take a look at what's going on on the network. I hit Add List. There's a call to API to-do list. Let me delete a few. We'll add one, Call Scott. We've got two different things going on here. We've got a call to api/TodoList, and we've got a call to api/Todo. I can look at the details of that and see what that request is. Here's the creation of Call Scott. All right? All of that being done on the client side with JavaScript calling these two controllers. We've got the Todo controller, and we've got the TodoList controller. So the entire application, the interaction with the entire application, is all via services. And on the client side, we're using JavaScript to do that work. Let's take a look at that. So the JavaScript for our—oops— application down in here and the AJAX calls are being done in jQuery. So here we've got a little helper method to go and call that with an AJAX request, and the data that's being pushed back and forth is all JavaScript. You can mix and match your applications. You don't have to have an application be just one that uses services. You may already have an existing ASP.NET app in web forms or MVC, and you want to go and add a service to it. You may want to go all the way in the other direction, make applications that provide rich and varied services, some that provide XML, some that provide JSON, and some that provide something that we don't even know about yet. You can mix and match all of these into one ASP.NET, and I hope that you feel really comfortable plugging these things in web forms. ASP.NET applications can have Web APIs. And MVC can have Web APIs. It's all part of one ASP.NET. Do go around and explore the ASP.NET website at All this information and lots, lots more, including sample apps, videos, and details in depth, are available. And I hope you have lots of fun playing with it. [Microsoft] []

Video Details

Duration: 9 minutes and 7 seconds
Country: United States
Language: English
License: All rights reserved
Genre: None
Views: 7
Posted by: neudesicasp on Aug 9, 2013

Scott Hanselman explains how to create services with ASP.NET Web API.

Caption and Translate

    Sign In/Register for Dotsub to translate this video.