• Random
  • Archive
  • RSS

- KaiXinS -

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:

using Microsoft.ApplicationServer.Http.Activation; using WebApi.APIs;

In the RegisterRoutes function we need the following,

like so:

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.


    • #Technology
    • #web.api
  • 1 year ago
  • 1
  • Permalink
Share

Short URL

TwitterFacebookPinterestGoogle+

1 Notes/ Hide

  1. kaixins posted this
← Previous • Next →

About

Avatar Hi! I'm Kai. This blog is mostly musings and learnings, it's about being a young developer and the projects I'm working on.
You might like one of these:
Technology /
Archive

Find me -

  • @KaiXinS on Twitter
  • KaiXinS on Last.fm
  • Google
  • Linkedin Profile

Twitter

loading tweets…

  • RSS
  • Random
  • Archive
  • Mobile
Effector Theme by Pixel Union