AmbiguousMatchException: The request matched multiple endpoints.

Dung Do Tien Jan 07 2021 655

I have an issue when working with Swagger in ASP.NET Core API

When I access https://localhost:5001/swagger/index.html to test my API. I got the issue bellow

Microsoft.AspNetCore.Routing.Matching.AmbiguousMatchException: The request matched multiple endpoints. 
Matches: JWTAuthenticationExample.Controllers.ProductsController.GetById (JWTAuthenticationExample) 
JWTAuthenticationExample.Controllers.ProductsController.GetByUrlSlag (JWTAuthenticationExample) 
at Microsoft.AspNetCore.Routing.Matching.DefaultEndpointSelector.ReportAmbiguity(CandidateState[] candidateState) 
at Microsoft.AspNetCore.Routing.Matching.DefaultEndpointSelector.ProcessFinalCandidates(HttpContext httpContext, CandidateState[] candidateState) 
at Microsoft.AspNetCore.Routing.Matching.DefaultEndpointSelector.Select(HttpContext httpContext, CandidateState[] candidateState) 
at Microsoft.AspNetCore.Routing.Matching.DfaMatcher.MatchAsync(HttpContext httpContext) 
at Microsoft.AspNetCore.Routing.Matching.DataSourceDependentMatcher.MatchAsync(HttpContext httpContext) 
at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext) 
at Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware.Invoke(HttpContext context) 
at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context) 
at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext) 
at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider) 
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

I don't know where my issue comes from. I wanna show Controller and Startup.cs 

 public class ProductsController : Controller
{
    private readonly IProductService _service;

    public ProductsController(IProductService service)
    {
        _service = service;
    }

    [HttpGet]
    public IActionResult Get()
    {
        return Ok(_service.GetProducts());
    }

    [HttpGet("/{id}")]
    public IActionResult GetById([FromQuery] int id)
    {
        return Ok(_service.GetProductById(id));
    }

    [HttpGet("/{urlSlag}")]
    public IActionResult GetByUrlSlag([FromQuery] string urlSlag)
    {
        return Ok(_service.GetProductByUrlSlag(urlSlag));
    }
}
 public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers();
        services.AddSwaggerGen();
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        app.UseSwagger();
        app.UseSwaggerUI(action => { action.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1"); });
        app.UseHttpsRedirection();
        app.UseRouting();
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }
}

Maybe someone can help me resolve this issue

Have 1 answer(s) found.
  • S

    Sandeep Kumar Jan 07 2021

    You have some mistake when designing API

    You have 2 API with the same URL : GetById and GetByUrlSlug

    The URL of both APIs is https://localhost:5001/products/xxx. So when you make a request to this URL. The route didn't know what is the target of the URL

    You must change the route of one of them to make sure you have only 1 API per URL

    For example:

    [HttpGet("GetById/{id}")]
    public IActionResult GetById([FromQuery] int id)
    {
    return Ok(_service.GetProductById(id));
    }
    
    [HttpGet("GetByUrlSlag/{urlSlag}")]
    public IActionResult GetByUrlSlag([FromQuery] string urlSlag)
    {
    return Ok(_service.GetProductByUrlSlag(urlSlag));
    }

    The URL will be changed to 
    https://localhost:5001/products/getbyid/xxx
    and 
    https://localhost:5001/products/getbyurlslag/xxx

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close