I started to experiment on ASP.NET Core WEB API and while writing a controller to assist multiple get requests which are differed by number of parameters,I am getting the below error.
Microsoft.AspNetCore.Routing.Matching.AmbiguousMatchException: The request matched multiple endpoints. Matches:
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) 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.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
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
So, my question is, can we have multiple get Methods
Below is my code:
[HttpGet]
public IEnumerable<Product> GetProducts()
{
return _Productcontext.GetProducts();
}
[HttpGet]
public Product GetProduct([FromQuery(Name = "ProductCode")]string ProductCode)
{
return _Productcontext.GetProductByProductCode(ProductCode);
}
Finally, Please direct me to any best model binding,routing in asp.net core articles and how they are different from the ASP.NET WEB-API.
Microsoft.AspNetCore.Routing.Matching.AmbiguousMatchException: The request matched multiple endpoints.
You can try to enable or disable an action for a given request based on query string that you passed by implementing a custom ActionMethodSelectorAttribute , like below.
[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);
}
}
}
Apply to your actions
[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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.