即使我为
request has been matched with the multiple endpoints
操作定义了不同的路线,我也会收到类似 httpGet
的错误。
代码如下所示,
[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);
}
}
请告诉我为什么会收到错误以及任何避免该问题的建议。
更新:错误详细信息
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)
以下是program.cs文件中的配置
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapControllerRoute(
name: "default",
pattern: "api/{controller}/{action}/{id?}");
endpoints.MapGet("/", () => "This is the sample project");
});
异常消息提到“/”存在冲突的端点/URL。
删除此行:
endpoints.MapGet("/", () => "This is the sample project");
来自
app.UseEndpoints()
应该可以解决您的问题。