1

我开始在 ASP.NET Core WEB API 上进行试验,并在编写控制器来协助多个参数数量不同的 get 请求时,我收到以下错误。

Microsoft.AspNetCore.Routing.Matching.AmbiguousMatchException:请求匹配多个端点。火柴:

PaperRocket.Controllers.Products.ProductsController.GetProducts (PaperRocket) PaperRocket.Controllers.Products.ProductsController.GetProduct (PaperRocket) at Microsoft.AspNetCore.Routing.Matching.DefaultEndpointSelector.ReportAmbiguity(CandidateState[] CandidateState) at Microsoft.AspNetCore.Routing.Matching .DefaultEndpointSelector.ProcessFinalCandidates(HttpContext httpContext, CandidateState[] CandidateState) 在 Microsoft.AspNetCore.Routing.Matching.DefaultEndpointSelector.Select(HttpContext httpContext, CandidateState[] CandidateState) 在 Microsoft.AspNetCore.Routing.Matching.DfaMatcher.MatchAsync(HttpContext httpContext)在 Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware 上的 Microsoft.AspNetCore.Routing.Matching.DataSourceDependentMatcher.MatchAsync(HttpContext httpContext)。在 Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext 上下文)调用(HttpContext httpContext)

HEADERS ======= Accept: / Accept-Encoding: gzip, deflate, br Cache-Control: no-cache Connection: keep-alive Host: localhost:54967 User-Agent: PostmanRuntime/7.24.1 Postman-Token: 0f3ce4b4-283a-472b-9350-7b0cc02d31ae

所以,我的问题是,我们可以有多个 get 方法吗

  1. 参数数量不同(在 WEB-API 中可能)
  2. 不使用 HTTPGET("RouteConstraint")-(我知道这个,它会起作用)
  3. 相应的方法应该被我们传递的参数调用和识别

下面是我的代码:

    [HttpGet]
    public IEnumerable<Product> GetProducts()
    {
        return _Productcontext.GetProducts();
    }

    [HttpGet]
    public Product GetProduct([FromQuery(Name = "ProductCode")]string ProductCode)
    {
        return _Productcontext.GetProductByProductCode(ProductCode);
    }

最后,请指导我在 asp.net 核心文章中的任何最佳模型绑定、路由以及它们与 ASP.NET WEB-API 的不同之处。

4

1 回答 1

0

Microsoft.AspNetCore.Routing.Matching.AmbiguousMatchException:请求匹配多个端点。

您可以尝试根据通过实现自定义ActionMethodSelectorAttribute传递的查询字符串为给定请求启用或禁用操作,如下所示。

[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class CheckProductCodeAttribute : ActionMethodSelectorAttribute
{
    public string QueryStingName { get; set; }
    public bool CanPass { get; set; }
    public CheckProductCodeAttribute(string qname, bool canpass)
    {
        QueryStingName = qname;
        CanPass = canpass;
    }

    public override bool IsValidForRequest(RouteContext routeContext, ActionDescriptor action)
    {
        StringValues value;

        routeContext.HttpContext.Request.Query.TryGetValue(QueryStingName, out value);

        if (QueryStingName == "" && CanPass)
        {
            return true;
        }
        else
        {
            if (CanPass)
            {
                return !StringValues.IsNullOrEmpty(value);
            }

            return StringValues.IsNullOrEmpty(value);
        }
    }
}

适用于您的操作

[HttpGet]
[CheckProductCode("", true)]
[CheckProductCode("ProductCode", false)]
public IEnumerable<Product> GetProducts()
{
    return _Productcontext.GetProducts();
}

[HttpGet]
[CheckProductCode("", false)]
[CheckProductCode("ProductCode", true)]
public Product GetProduct([FromQuery(Name = "ProductCode")]string ProductCode)
{
    return _Productcontext.GetProductByProductCode(ProductCode);
}
于 2020-04-20T02:33:44.360 回答