Introduction to ASP.NET Web API

Actually it’s not much of an introduction for me, but there will still be some trace of progress. I use old good stable .NET (Coreless). I only need an API from there, so I’ve created a project of type ASP.NET Web Application and selected an empty template with references to Web API. Empty is empty, only with a very basic folders structure, so nothing works there out of the box, even a controller must be added first. By default, if controller’s methods are named after HTTP verbs, e.g. Get(), or even only start with such names, they automatically serve corresponding requests. How to find the correct controller and what are the action’s parameters? This is defined using URL templates, just like in the sample route automatically defined in WebApiConfig.cs:

config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);

So when a request goes to “/api/Something/123”, the routing mechanism will look for SomethingController (the suffix is added by convention), then inside it – for an action corresponding to the HTTP verb. The action can have an “id” parameter, preferably of the same type as the actual parameter value. If something goes wrong here, an error will occur. There is a nice tool called Postman which is designed for testing APIs manually – we can send a request directly to a given address with given data and see a response. It is available as a desktop application (which I use) or as a Chrome extension. There is a paid version, but a free one exists as well and is already full of features, so there’s rather no problem for me.

For basic CRUD the above routing method is sufficient. But if we are accustomed to more descriptive names or there are more actions for the same HTTP verb within a controller – there is a different configuration available. It was introduced in a later version of Web API and is called attribute routing. As the name says – we can put URL templates in attributes for controllers: RoutePrefix(“path/to/controller”) and for actions: Route(“path/to/action”). An optional controller’s template is concatenated with each action’s template and, since there is no binding to an HTTP verb by convention, there must be an additional attribute added to determine it, e.g. HttpGet. To turn on attribute routing, “config.MapHttpAttributeRoutes();” must be called in WebApiConfig.Register. Both solutions can be combined:

[RoutePrefix("first")]
public class FirstController : ApiController
{
public IEnumerable<string> GetValues()
{
return new List<string>() { "get 1", "get 2" };
}

[Route("take")]
[HttpGet]
public IEnumerable<string> TakeValues()
{
return new List<string>() { "take 1", "take 2" };
}
}

GetValues() will then be called by requesting “/api/first”, whereas TakeValues() needs “/first/take”.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s