Join Michael Lehman for an in-depth discussion in this video Building web APIs, part of Visual Studio 2013 for Web Developers.
With Visual Studio and ASP.NET, there are multiple ways to create web services. The traditional Microsoft only way that you could do this, was by using Windows Communication Foundation. Visual studio 2013 still fully supports WCF, and allows you to create WCF service libraries, Service Applications, WCF Applications with Workflow built in, and Syndication Service Libraries. Most people today build web APIs using what's called R.E.S.T., And R.E.S.T. stands for Representational State Transfer.
The whole idea is that you have state with services on the back end, and you can access them by creating complex URIs, which communicate not only what you want to do, but also how you want to do it. So let's go ahead and create another project. In this case, we're just going to pick Web API, and you can learn more about using RESTful services here. We're going to create a Web API that doesn't have any authentication. We're just going to go ahead and click OK. Now, what we see here, is that we not only got the basic structure of an ASP.NET MVC application, we also ended up with what's called the Values Controller.
If we go and take a look at that, we'll see that we now have a collection of methods that come in a variety of ways. We have a Get method, a Get method with a parameter, a Post method, and a Put method. And up above each method, you see a sample URI that we use to access this particular thing. So, for example, you would put in your website address, /api/values, and that would return this IEnumerable of whatever you wanted to return here, in this case, an array of strings. If you wanted to get a specific value, you could specify values and a number, and you do that by specifying that a different signature here takes an integer.
If we created an additional one, let's say we created public string Get (string foo), and we returned foo, we would now get that that with // GET api /values/<string>. So let's go ahead and try this. By default, the web, API template also gives you a simple ASP.NET NVC application. So here you can put some information about the application if you were only doing web services. The reason it's done this way, is because you typically combine ASP.NET NVC applications that access your data from a web page point of view, with web services that access it via REST.
And in our case, first of all, we're just going to check it out and make sure it's working. API values, and there's our values, value1, value2. Now, if we put in values 5, we're going to go ahead and get back that value, value because, remember, we looked back here with an integer parameter, it returns the word value. And, if we now put in values Fubar, we're going to get back an error message. You're wondering to yourself, Why is that the case? Well if it turns out if we go back and look at our code, we said that we had one signature that said return to string, method named Get, with a parameter named id, and another method named Get also with a different parameter named foo.
It turns out that the Web API mechanism is automatically taking the very first parameter if you don't use query parameters, and setting it for the ID value. So, your only choice, in this particular case, if you want to have multiple implementations like this, is you have to explicitly use a query parameter or only will have one Get method. So, let's go back to our page here, and if we simply say foo, well we make it a query parameter. Foo equals FOOBAR. There we go. Now returns our string, FOOBAR. FOOBAR is GUT, and there we go.
You can see that we're actually passing that string parameter in there. So the whole idea of ASP.NET web API's, is that it makes it incredibly easy to do REST based web services. So you can not only do POST and PUT, you can also do DELETES. Now the key thing here is that if you don't include all these method signatures, you can have controllers, for example, that are read only. If you don't provide a POST or a PUT or a DELETE method, all that external requesters can do, is read. Or of course you can also have controllers that all you can do is write.
So for example, if you had a some sort of logging service, it might not have any API to retrieve values, it might only be a data sync. And that's a quick look at what's going on with ASP.NET, and Web API in Visual Studio 2013.
- What's new in Visual Studio 2013?
- Exploring the editor
- Invoking the compiler
- Using the debugger
- Understanding One ASP.NET
- Building web APIs
- Debugging locally on IIS
- Using One-Click Publish