Start programming with web.api and ASP.NET MVC
Right! Now we have a brand new web.api project set up.
The first thing we want to do is create a new folder in our project, and call it APIs. This is where we will put our Service Contracts - the classes that define which service endpoints our API will expose.
In this folder create a new class. For this example, we will use a simple task list, so I called my new class ‘TaskApi’.
The basic code for our TaskApi is this:
We also need to register our api as an ASP.NET Route in the Global.asax file by adding the following code:
In the RegisterRoutes function we need the following,
We register our route with so ’api/…’ to avoid any confusion with our other MVC routes.
We can now get access to our API through the browser. Set a specific port with which to test your API. To do this, right click on the project and select properties. Choose the ‘Web’ tab. Under ‘Servers’, specify a specific port (e.g. 9000) and save.
Our API can now be accessed at “http://localhost:9000/api/tasks”. Before we get excited though, we need something to test! In the next post we will set up a Task Resource and expose it using HTTP GET.