Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Introducing Web API4:25 with James Churchill
Before we start working on our project, let's review what the Web API framework is and why we would want to use it to create services or APIs.
To follow along committing your changes to this course, you'll need to fork the aspnet-fitness-frog-spa repo. Then you can clone, commit, and push your changes to your fork like this:
git clone <your-fork> cd aspnet-fitness-frog-spa git checkout tags/v1.5 -b introducing-web-api
Customizing Web API
ASP.NET Web API provides a number of ways that it can be customized or extended. See the following resources for more information:
- HTTP Message Handlers
- Media Formatters
- Type Converters
- Model Binders
- Value Providers
- HttpActionResult Types
Self-Hosting Web API
Throughout this course, we'll be using IIS Express—a lightweight version of Microsoft's IIS (Internet Information Services) web server—to host our Web API application. But you can also host your Web API applications outside of IIS, using a feature called "self-hosting". For more information about how to self-host Web API applications, see the following resource:
Returning JSON from an MVC Controller
As mentioned, it's possible to use ASP.NET MVC to return responses containing JSON formatted data. For more information on how to do that, see these resources:
We're almost ready to add the Web API framework to our project and
start building out our service.
But you might be wondering, what exactly is the Web API framework.
And why would we want to use it to create services or APIs?
First, let's take a look at how Web API handles HTTP messages.
When a client, such as a mobile app, desktop app, or web page,
makes a request to our API, Web API uses the first segment of the path,
the resource name, to determine which API controller to instantiate.
After the controller has been instantiated, Web API then uses
the request http method to determine which action method on the controller to call.
Web API's Parameter Binding maps route parameter values, query string
parameter values, and message buddy content to the action methods parameters.
The controller's action method processes the request, which typically involves
retrieving, persisting, or deleting data to or from a data store, like a database.
The action method returns a response that includes an HTTP status code and
optionally some data.
If the response includes data,
Web API will serialize that data into the format that was requested by the client.
Or into the JSON data format, if the format wasn't specified.
This entire process is very flexible.
Web API provides a number of extensibility points.
And entire parts of the process can be replaced with our own
But how to do this is beyond the scope of this course.
If you want to learn more about how to customize Web API,
see the teacher's notes for more information.
If you've worked with ASP.NET MVC,
you'll see similarities between that web framework and Web API.
Both frameworks make use of routing, controllers, and
action methods, just to name a few items.
But to be clear, Web API is completely separate from ASP.NET MVC.
In fact, Web API has its own name space, system web HTTP
separate from the namespace that MVC uses, system web MVC.
Even though Web API and MVC are separate frameworks,
they're also complementary to each other.
It can happily coexist in the same application.
Including both frameworks in the same project is a commonly used approach
that allows you to have a set of MVC controllers for handling UI related
requests and a set web API controllers for handling API requests.
If you're an MVC developer, you might be familiar with the JSON controller-based
class helper method and the JSON result ActionResult type.
Both of which can be used to return responses containing JSON formatted data.
So given that MVC can work with JSON formatted data,
is it possible to use NVC to build our service?
Do we really need to use Web API?
While it would be possible to build our service using MVC,
the Web API framework was created specifically for building HTTP-based APIs.
Given that, has a number of capabilities that make it uniquely suited for
Web API was built around the HTTP protocol, and its messaging semantics.
This allows it to embrace the conventions of REST.
It's easy to create APIs that expose resource focused end points, and
associate controller action methods with specific HTTP methods.
The API controller base class, also contains a collection of helper methods
that allow you to easily return responses with appropriate HTTP status codes.
Web API provides powerful and
flexible data serialization capabilities in the form of media formatters.
Web API uses media formatters to abstract away the responsibility of
serialization from your controllers.
This approach enables a feature known as content negotiation, which allows
requesters to specify the preferred data format via the accept header.
In the next section,
we'll take a closer look at all of these web API features and more.
You need to sign up for Treehouse in order to download course files.Sign up