Web API Attribute Routing

In my continue research into Web API I came across a new feature in Web API 2 called Attribute Routing.  This feature makes it possible to define routes at the API level.  It makes it easier to define complex RESTful URIs where there are child resources like “/customer/123/orders”.  It also gives you the ability to have multiple GETS, POSTS and PUTS on the same controller.

The traditional style of routing is called Convention-Based Routing and you can mix both in the same application.

On a side note, there is also the ActionName attribute.  It doesn’t give you the ability to define the route but it does make it possible for you to have multiple GETS, POSTS and PUTS on the same controller.

You do have to enable attribute routing.  This is done in the WebApiConfig.Register method: config.MapHttpAttributeRoutes();

This ASP.NET link describes attribute routing in more detail: http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2

Web API with multiple post apis on a controller

I was researching how to add multiple post APIs on a controller and found a solution that seems to be working.  I found this on this post: http://e-zooka.com/mvc4-webapi-multiple-post-get-actions/

public class EnrollmentController : ApiController
{
[System.Web.Http.AcceptVerbs(“GET”, “POST”), System.Web.Http.ActionName(“MyApi1”)
public Result MyApi1([FromBody] MyObject parameter)
{
var result = new Result
{
Code = “0”,
Message = “Success”,
TimeStamp = DateTime.UtcNow.ToString()
};
return result;
}

[System.Web.Http.AcceptVerbs(“GET”, “POST”), System.Web.Http.ActionName(“MyApi2”)
public Result MyApi2([FromBody] MyObject parameter)
{
var result = new Result
{
Code = “0”,
Message = “Success”,
TimeStamp = DateTime.UtcNow.ToString()
};
return result;
}
}

public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// To handle routes like /Mobile
config.Routes.MapHttpRoute(
name: “ControllerOnly”,
routeTemplate: “Api/{controller}”
);

// Controller with ID
// To handle routes like /api/Mobile/1
config.Routes.MapHttpRoute(
name: “ControllerAndId”,
routeTemplate: “Api/{controller}/{id}”,
defaults: null,
constraints: new { id = @”^\d+$” } // Only integers
);

// Controllers with Actions
// To handle routes like /api/Mobile/actionname
config.Routes.MapHttpRoute(
name: “ControllerAndAction”,
routeTemplate: “Api/{controller}/{action}”
);

// Uncomment the following line of code to enable query support for actions with an IQueryable or IQueryable return type.
// To avoid processing unexpected or malicious queries, use the validation settings on QueryableAttribute to validate incoming queries.
// For more information, visit http://go.microsoft.com/fwlink/?LinkId=279712.
//config.EnableQuerySupport();

// To disable tracing in your application, please comment out or remove the following line of code
// For more information, refer to: http://www.asp.net/web-api
//config.EnableSystemDiagnosticsTracing();
}
}