I am getting an error like request has been matched with the multiple endpoints
even if I have different routes defined for httpGet
operations.
The code looks like as below,
[Route("api/[controller]")]
[ApiController]
public class CrmManagementController : ControllerBase
{
[HttpGet("projects")]
public async Task<ActionResult> ProjectInfo(
[FromServices] ICrmProjects crmProjects,
CancellationToken cancellationToken = default)
{
_ = crmProjects ?? throw new ArgumentNullException(nameof(crmProjects));
var vantagepointProjects = await crmProjects.GetAllVantagepointProjectsByNumberAsync(cancellationToken).ConfigureAwait(false);
return Ok(vantagepointProjects.Values);
}
[HttpGet("pursuits")]
public async Task<ActionResult> PursuitInfo(
[FromServices] ICrmProjects crmProjects,
CancellationToken cancellationToken = default)
{
_ = crmProjects ?? throw new ArgumentNullException(nameof(crmProjects));
var vantagepointPursuits = await crmProjects.GetAllVantagepointPursuitsByNumberAsync(cancellationToken).ConfigureAwait(false);
return Ok(vantagepointPursuits.Values);
}
}
Please let me know why I am getting the error and any suggestions to avoid the problem.
Update: Error details
fail: Microsoft.AspNetCore.Server.Kestrel[13]
Connection id "0HN1DKQDVNS2Q", Request id "0HN1DKQDVNS2Q:00000001": An unhandled exception was thrown by the application.
Microsoft.AspNetCore.Routing.Matching.AmbiguousMatchException: The request matched multiple endpoints. Matches:
HTTP: GET /
HTTP: GET / at Microsoft.AspNetCore.Routing.Matching.DefaultEndpointSelector.ReportAmbiguity(Span`1 candidateState)
at Microsoft.AspNetCore.Routing.Matching.DefaultEndpointSelector.ProcessFinalCandidates(HttpContext httpContext, Span`1 candidateState)
at Microsoft.AspNetCore.Routing.Matching.DefaultEndpointSelector.Select(HttpContext httpContext, Span`1 candidateState)
at Microsoft.AspNetCore.Routing.Matching.DfaMatcher.MatchAsync(HttpContext httpContext)
at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Microsoft.WebTools.BrowserLink.Net.BrowserLinkMiddleware.InvokeAsync(HttpContext context)
at Microsoft.AspNetCore.Watch.BrowserRefresh.BrowserRefreshMiddleware.InvokeAsync(HttpContext context)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application)
Below is the configuration in program.cs file
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapControllerRoute(
name: "default",
pattern: "api/{controller}/{action}/{id?}");
endpoints.MapGet("/", () => "This is the sample project");
});
The exception message mentions that there are conflicting endpoints/URLs for "/".
By removing this line:
endpoints.MapGet("/", () => "This is the sample project");
from the app.UseEndpoints()
should resolve your issue.