Azure-docs: Is there a way to order routes?

Created on 16 Jul 2018  Â·  13Comments  Â·  Source: MicrosoftDocs/azure-docs

I have two APIs: users\{id} and users\recent. I'm trying to call the latter one, but it always calls the former one instead. Is there a way to set order or priority?


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

awaiting-product-team-response cxp functionsvc in-progress product-question triaged

Most helpful comment

What if you have a routing like:
survey/{id}
survey/{id}/pay

I'd like to order the later first. As of now I'm having the same prob as described above. When I call survey/{id}/pay it is hitting the first route :(

All 13 comments

@MisinformedDNA Thanks for the feedback. We are actively investigating and will get back to you soon.

Not sure I fully understand the problem. How are you calling these APIs? Are you saying regardless of the GET command you always get the same API call back even when specific a different API ID?

There are two functions in the same app, both have a GET verb and both have defined routes:

        [FunctionName("GetRecent")]
        public static IActionResult GetRecent(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "lists/recent")]HttpRequest req)
        { ... }

        [FunctionName("GetList")]
        public static IActionResult Get(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "lists/{id}")]HttpRequest req,
            string id)
        { ... }

As you can see, these routes overlap when id equals recent. To avoid these issues, ASP.NET MVC would let us define a route order, but I don't see that option here. So how does Azure Functions know which function to run? Right now, whenever I make a GET request to https://localhost:7071/api/lists/recent, it always accesses GetList instead of GetRecent, regardless of the order they are defined. Based on a guess, they may be running in alphabetical order of the FunctionName, but I'm not sure.

NOTE: The APIs are being called from .NET apps and the browser with the same result.

Thanks for that. I am checking with the functions team offline to get an answer.

@MisinformedDNA , are you observing this on the V1 or V2 Functions runtime?

@kashimiz v2

@MisinformedDNA the ability to define route order is not exposed today. My recommendation for your scenario, for now, would be to add an appropriate route constraint (e.g. a constraint to your id in the route template) to avoid ambiguity. That would prevent having recent qualify as an id.

Oh OK, so I could do something like

Route = "lists/{id:minlength(10)}"

That'll work for me.

@fabiocav Is there an issue tracking this?

This tracks the validation to detect ambiguous routes, which is similar to what we had in V1:
https://github.com/Azure/azure-functions-host/issues/3152

Implicit route priority (most to least restrictive) is another enhancement to be made, but we don't have anything tracking explicit order/priority.

Unfortunately can't use Route constraints for a guid id. It fails to bind it to a Guid parameter!

@audipen See https://docs.microsoft.com/en-us/aspnet/core/fundamentals/routing?view=aspnetcore-2.1#route-constraint-reference. You should be able to use {id:guid}. Or, alternately, you can accept a string and convert to a Guid yourself.

What if you have a routing like:
survey/{id}
survey/{id}/pay

I'd like to order the later first. As of now I'm having the same prob as described above. When I call survey/{id}/pay it is hitting the first route :(

Was this page helpful?
0 / 5 - 0 ratings

Related issues

behnam89 picture behnam89  Â·  3Comments

mrdfuse picture mrdfuse  Â·  3Comments

Agazoth picture Agazoth  Â·  3Comments

jebeld17 picture jebeld17  Â·  3Comments

Ponant picture Ponant  Â·  3Comments