0

I used to have a NET Web API (4.6.2) application that had some ambiguous routes. Something like this:

[RoutePrefix(RoutePrefix)]
public class TestController : ApiController
{
public const string RoutePrefix = "test";

[HttpGet, Route("")]
public IHttpActionResult GetAll(Guid uid, [FromUri]int limit, [FromUri]int offset)
{
  // Logic Here.
}

[HttpGet, Route]
public async Task<IHttpActionResult> Test([FromUri]Guid id, [FromUri]int? limit = null, [FromUri]int? offset = null)
{
  // Logic Here.
}
}

All of this was working on the past because I had this setting on my Startup:

config.MapHttpAttributeRoutes();

The problem is that I'm converting this API to a NET Core MVC API and the MapHttpAttributeRoutes setting is not available anymore. This means that right now I'm having some ambiguity issues on some of the actions (like the one mentioned above).

I know that one way around (And I want to do that in the future) is to have a specific route for every action. At this point and based on the usage of this API I cannot change all the routes. With all this, I was wondering if someone found a way to handle this situation without changing the routes?

Thank you guys.

1 Answer 1

2

Ambiguous route are not supported by ASP.NET Core, and actually aren't supported by later versions of even Web Api, as far as I'm aware. In ASP.NET Core, specifically, routing short-circuits, so it will always take the first viable match and never consider anything else. You're only option is to assign unique routes to each action or to combine the logic of the two actions into one action.

That said, it doesn't make sense why you have two different actions here in the first place. There's no functional difference between the two, except the first will fill defaults in the params, while the second will fill nulls. All action params are optional by default. The model binder will bind default/null to each param automatically, if there's nothing from the request to fill in. If you want the last two params to be nullable, then just use that signature and then you can branch within the action on whether or not they have a value.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.