Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web API Routes to support both GUID and integer IDs

How can I support GET routes for both GUID and integer? I realize GUIDs are not ideal, but it is what it is for now. I'm wanting to add support for integers to make it easier for users to remember and communicate what should be unique "keys."

Example routes:

testcases/9D9A691A-AE95-45A4-A423-08DD1A69D0D1   
testcases/1234

My WebApiConfig:

public static void Register(HttpConfiguration config)
{
    config.MapHttpAttributeRoutes();
    var routes = config.Routes;

    routes.MapHttpRoute("DefaultApiWithAction", 
        "Api/{controller}/{action}");

    routes.MapHttpRoute("DefaultApiWithKey",
        "Api/{controller}/{key}",
        new { action = "Get" },
        new { httpMethod = new HttpMethodConstraint(HttpMethod.Get), key = @"^\d+$" });

    routes.MapHttpRoute("DefaultApiWithId", 
        "Api/{controller}/{id}", 
        new { action = "Get" }, 
        new { httpMethod = new HttpMethodConstraint(HttpMethod.Get) });

    routes.MapHttpRoute("DefaultApiGet", 
        "Api/{controller}", 
        new { action = "Get" }, 
        new { httpMethod = new HttpMethodConstraint(HttpMethod.Get) });

    routes.MapHttpRoute("DefaultApiPost", 
        "Api/{controller}", 
        new { action = "Post" }, 
        new { httpMethod = new HttpMethodConstraint(HttpMethod.Post) });
}

My controller (method signatures only):

[RoutePrefix("Api/TestCases")]
public class TestCasesController : PlanControllerBase
{
    [Route("")]
    public OperationResult<IEnumerable<TestCaseDTO>> Get([FromUri] TestCaseRequest request)

    [Route("{id}")]
    [HttpGet]
    public OperationResult<TestCaseDTO> Get(Guid id)

    [Route("{key}")]
    [HttpGet]
    public OperationResult<TestCaseDTO> Get(int key)

    ...
}

I'm getting an Internal Server Error when I attempt to call the resource using the integer. Any help is appreciated!

like image 689
gopherr Avatar asked Jul 23 '15 03:07

gopherr


People also ask

Which types of routing is supported in Web API?

Routing is how Web API matches a URI to an action. Web API 2 supports a new type of routing, called attribute routing. As the name implies, attribute routing uses attributes to define routes. Attribute routing gives you more control over the URIs in your web API.

What are the two ways to implement routing in Web API?

It routes an incoming HTTP request to a particular action method on a Web API controller. Web API supports two types of routing: Convention-based Routing. Attribute Routing.

What is GUID REST API?

GUID stands for Global Unique Identifier. Many times it will also be referred to as UUID or Universally Unique Identifier. There is no hard science in decoding the meaning of GUID by its name. A GUID is an identifier which is globally unique.


1 Answers

Thank you to @SirwanAfifi! I had come across the Attribute Routing in ASP.NET article referred to in the SO question you mentioned, but apparently I didn't see the need for route attribute constraints at the time.

For me, it was using [Route("{id:guid}")] and [Route("{key:int}")] on my controller methods that did the trick. I also commented out the Http routes related to {id} and {key} in my WebApiConfig to verify that the attributes in the controller are responsible for doing the routing.

like image 53
gopherr Avatar answered Sep 20 '22 02:09

gopherr