Building Web APIs with ASP.NET Core — Attribute routing

Carlos Gómez
devops and cross platform development
3 min readJul 6, 2016

--

Let’s review what this url http://localhost:51071/api/values means, where did “/api/values” come from? What made this get a request from the browser to arrive at that particular action on my controller? All this was done by attribute routing applying attributes to the controllers and actions to specify how a request should be routed due to an action.

[Route("api/[controller]")]
public class ValuesController : Controller

In this case the attribute [Route(“api/[controller]”)] is defining that when we browse to “http://localhost:51071/api/values" we should be routed to the “ValuesController”.
The [controller] it’s a new feature in ASP.NET core called route tokens, we often want the route to map to the controller name so traditionally we used to type in [Route(“api/values”)] in this route but if you refactored your controller name you have to change the route also, but because we’re using a route token no matter the name of the controller, [Route(“api/[controller]”)] will route to the correct controller.

There are also attribute routes on each of the actions as [HttpGet],[HttpPost],[HttpPut],[HttpDelete], we could define additional information to the attribute as [HttpGet(“{id}”)].
The way attribute routes on actions work is they are actually concatenated with whatever routes you specified on the controller so for example the full route for the attribute [HttpGet(“{id}”)] could be something like “/api/values/1”.
The curly braces [HttpGet(“{id}”)] mean that there is a route parameter and this is the value that we want to extract from the URI when it comes in and matches this route to make it available to my application, the model binding system in ASP.NET core will automatically map this ID route parameter to the parameter of the action methods just by name.

[HttpGet("{id}")]
public string Get(int id)
{
return $"value {id}";
}

So if we go to “http://localhost:51071/api/values/123" we should see

Example of web api attribute routing

But what would happen if you type “ABC” instead of an ID as in “http://localhost:51071/api/values/ABC", well the action method is binding the ID to an int Get(int id) and “ABC” isn’t an int so, model binding will detect that those things should align, in this case the type don’t match, it won’t actually fail, but you’ll basically get a model validation error and you’ll just get a default value for that int.

Web api attribute routing returning a default value

How can we fix that to only receive an int value for the ID for this particular action method? This could be done with inline constraints [HttpGet(“{id:int}”)] which is going to force the binding with whatever we want.

[HttpGet("{id:int}")]
public string Get(int id)
{
return $"value {id}";
}

Now if we browse to “http://localhost:51071/api/values/ABC" we should get a “404” because the type doesn’t match any more.

Source code: https://github.com/devcfgc/web-api-asp-net-core

Part one: Building Web APIs with ASP.NET Core
Part two: Building Web APIs with ASP.NET Core — Attribute routing

--

--

Cloud/Software Architect and DevOps learning about #devops, #cloud, #netcore, #microservices and #newtech