Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set route's Order precedence in ,NET 6 Minimal APIs using RouteEndpointBuilder ASP.NET Core 6 #39241

Closed
1 task done
sanasz91mdev opened this issue Dec 30, 2021 · 3 comments
Closed
1 task done

Comments

@sanasz91mdev
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I am trying to set order of Routes (with same path : api/v1/users ) defined in minimal APIs using app.Map(..) by giving Order to both definitions so that at a time only one is used

i am using followinfg code:

    var endPt1 = new RouteEndpointBuilder(emptyDelegate, RoutePatternFactory.Parse("/api/v1/users"), 1);
    app.Map(endPt1.RoutePattern, getUser1);

    var endPt2 = new RouteEndpointBuilder(emptyDelegate, RoutePatternFactory.Parse("/api/v1/users"),2);
    app.Map(endPt2.RoutePattern, getUser2);

In RoutePattern.InboundPrecedence the value is same in both endPt1, endPt2.

Is this a bug?
How can i set order correctly so that even in minimal APIs ... at a time with same path, only one route is called with lower order Precedence?

Expected Behavior

Route configured with lower precedence should be executed

in code below ..

    var endPt1 = new RouteEndpointBuilder(emptyDelegate, RoutePatternFactory.Parse("/api/v1/users"), 1);
    app.Map(endPt1.RoutePattern, getUser1);

    var endPt2 = new RouteEndpointBuilder(emptyDelegate, RoutePatternFactory.Parse("/api/v1/users"),2);
    app.Map(endPt2.RoutePattern, getUser2);

endPt1 should have been executed, but i am getting exception

Steps To Reproduce

github link
Code path: \AppNet6\Modules

Exceptions (if any)

An unhandled exception occurred while processing the request.
AmbiguousMatchException: The request matched multiple endpoints. Matches:

/api/v1/users => getUser1
/api/v1/users => getUser2
Microsoft.AspNetCore.Routing.Matching.DefaultEndpointSelector.ReportAmbiguity(CandidateState[] candidateState)

.NET Version

6.0.101

Anything else?

No response

@pranavkm
Copy link
Contributor

app.Map(endPt2.RoutePattern, getUser2);

You're not really using the builder here though, just the pattern literal. One way to go about this is to configure the EndpointBuilder via a convention:

app.Map("/api/v1/users", emptyDelegate).Add(static builder => ((RouteEndpointBuilder)builder).Order = 1);

@sanasz91mdev
Copy link
Author

sanasz91mdev commented Dec 31, 2021

app.Map(endPt2.RoutePattern, getUser2);

You're not really using the builder here though, just the pattern literal. One way to go about this is to configure the EndpointBuilder via a convention:

app.Map("/api/v1/users", emptyDelegate).Add(static builder => ((RouteEndpointBuilder)builder).Order = 1);

Thanks @pranavkm , this worked

@sanasz91mdev
Copy link
Author

Closed with response of @pranavkm

@ghost ghost locked as resolved and limited conversation to collaborators Jan 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants