0

I am trying to find the difference in terms of how URL matching is different in both these frameworks

I know the following is true for ASP.Net Web API, would like to know is it true for ASP.NET core also?

  1. Action name used to derive the HTTP method if it's not explicitly specified, for e.g

    public int GetById(int Id);

    Here no HTTP method specified but due to convention it will only be matched to get requests

  2. If HTTP method is not specified and it cannot be derived from the action name also, then the default is POST

  3. Parameters also participate in matching the URL , like below

    // GET api/values/get
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }
    
    // GET api/values/get/5
    public string Get(int id)
    {
        return "value";
    }
    

    The above code works fine in ASP.Net Web API but it gives the following exception in ASP.NET Core

    AmbiguousActionException: Multiple actions matched.

    ASP.Net Web API Route

    enter image description here

    ASP.Net CORE Web API Route enter image description here

    Note: I know how to fix the error, would like to know what has changed in ASP.Net Core's URL matching algorithm.

10
  • 1
    Please show how you configured your routing in both cases. Commented Mar 28, 2021 at 7:42
  • Edit the question with route details Commented Mar 28, 2021 at 8:01
  • Which version of .Net core are you using? UseMvc is old (2.0). Commented Mar 28, 2021 at 8:03
  • 2.1, but I feel the behaviour will be the same in the current version too. Commented Mar 28, 2021 at 8:11
  • the ambiguous error is raised only if you use the url /api/values/get. Actually this new behavior in the asp.net core makes more sense and I'm not so sure if the old asp.net is different as probably wrongly assumed by you. Wrong assumption happens everywhere. Commented Mar 28, 2021 at 8:35

1 Answer 1

0

Please check this article, in asp.net core application, Conventional routing typically used with controllers and views, REST APIs should use attribute routing.

If you're building APIs, the [ApiController] attribute should be applied to your controllers. Among other things, this attribute requires the use of attribute routing for actions in such controller classes. Attribute routing in ASP.NET Core behaves similarly in ASP.NET MVC and Web API. In addition to supporting the [Route] attribute, however, route information can also be specified as part of the HTTP method attribute:

[Route("api/[controller]")]
[ApiController]
public class TodoController : ControllerBase
{
    // GET: api/<TodoController>
    [HttpGet]
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }

If you want to use the conventional routing for the API controller and action, you need to remove the [ApiController] and [Route] attribute from the API controller, if that is the case, it will look as an MVC controller.

More detail information, see Routing differences between ASP.NET MVC and ASP.NET Core.

Then, for the URL matching, in asp.net core application, URL matching operates in a configurable set of phases. In each phase, the output is a set of matches. The set of matches can be narrowed down further by the next phase. The routing implementation does not guarantee a processing order for matching endpoints. All possible matches are processed at once. The URL matching phases occur in the following order. ASP.NET Core:

  1. Processes the URL path against the set of endpoints and their route templates, collecting all of the matches.
  2. Takes the preceding list and removes matches that fail with route constraints applied.
  3. Takes the preceding list and removes matches that fail the set of MatcherPolicy instances.
  4. Uses the EndpointSelector to make a final decision from the preceding list.
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.