AmbiguousMatchException:请求匹配多个端点。火柴?

AmbiguousMatchException: The request matched multiple endpoints. Matches?

我正在编写 .Net Core WebAPI,这里我正在编写两种 HttpGet 方法,一种是获取所有记录而不考虑参数,另一种是使用参数(大约 25 个参数),但我得到了 AmbigousMatchException错误。

我该如何解决这个问题?

下面是代码。

[Route("api/[controller]")]
[ApiController]
public class SummaryReportController : Controller
{
    DataProvider dp = new DataProvider();

    private readonly IConfiguration _configuration;
    public SummaryReportController(IConfiguration configuration)
    {
        _configuration = configuration;
    }

    [HttpGet]
    public JsonResult GetSummaryReport()
    {
        dp.ConnectionString = _configuration.GetConnectionString("conAccounting_SQLWeb");

        DataSet ds = new DataSet();
        ds = dp.GetDataSetSProc("uspFreight_SearchFreight");

        DataTable dt = ds.Tables[0];

        return new JsonResult(dt);
    }

    [HttpGet]
    public JsonResult GetSummaryReportWithParams(SummaryReport sm)
    {
        dp.ConnectionString = _configuration.GetConnectionString("conAccounting_SQLWeb");

        DataSet ds = new DataSet();
        SqlParameter[] paramChk = new SqlParameter[21];
        paramChk[0] = new SqlParameter("@VendIDName", sm.VendID);
        paramChk[1] = new SqlParameter("@BOLNumber", sm.BOLNumber);
        paramChk[2] = new SqlParameter("@BOLWeightFrom", sm.BOLWeightFrom);
        paramChk[3] = new SqlParameter("@BOLWeightTo", sm.BOLWeightTo);
        paramChk[4] = new SqlParameter("@InvoiceAmtFrom", sm.InvoiceAmountFrom);
        paramChk[5] = new SqlParameter("@InvoiceAmtTo", sm.InvoiceAmountTo);
        paramChk[6] = new SqlParameter("@DistanceFrom", sm.DistanceFrom);
        paramChk[7] = new SqlParameter("@DistanceTo", sm.DistanceTo);
        paramChk[8] = new SqlParameter("@InvoiceDateFrom", sm.InvoiceDateFrom);
        paramChk[9] = new SqlParameter("@InvoiceDateTo", sm.InvoiceDateTo);
        paramChk[10] = new SqlParameter("@ShipmentDateFrom", sm.ShipmentDateFrom);
        paramChk[11] = new SqlParameter("@ShipmentDateTo", sm.ShipmentDateTo);
        paramChk[12] = new SqlParameter("@ShipperZip", sm.ShipperZip);
        paramChk[13] = new SqlParameter("@ConsigneeCityState", sm.ConsigneeCityState);
        paramChk[14] = new SqlParameter("@ConsigneeZip", sm.ConsigneeZip);
        paramChk[15] = new SqlParameter("@ConsigneeCode", sm.ConsigneeCode);
        paramChk[16] = new SqlParameter("@ConsigneeName", sm.ConsigneeName);
        paramChk[17] = new SqlParameter("@WhseID", sm.WhseID);
        paramChk[18] = new SqlParameter("@LTLFTL", sm.LTLFTL);
        paramChk[19] = new SqlParameter("@TRSO", sm.TRSO);
        paramChk[20] = new SqlParameter("@FTL", sm.FTL);
        ds = dp.GetDataSetSProcWithProc("uspFreight_SearchFreight", paramChk);

        DataTable dt = ds.Tables[0];

        return new JsonResult(dt);
    }

}

将参数添加到第二个端点的路由[HttpGet("{sm}")]

尝试将 [Route("api/[controller]")] 更改为 [Route("api/[controller]/[action]")] 那么SummaryReportController中的动作默认路由为api/SummaryReport/{actionname}。路由可以参考官方doc

例如GetSummaryReport动作的路线为api/SummaryReport/GetSummaryReport,GetSummaryReportWithParams动作的路线为api/SummaryReport/GetSummaryReportWithParams....

如果你想从查询中得到 SummaryReport sm,你可以在 it.Here 之前添加 [FromQuery] 是一个工作演示:

[Route("api/[controller]/[action]")]
    [ApiController]
    public class SummaryReportController : ControllerBase
    {
        [HttpGet]
        public void GetSummaryReport()
        {

            
        }
        [HttpGet]
        public void GetSummaryReportWithParams([FromQuery]SummaryReport sm)
        {
           
        }

    }
    public class SummaryReport
    {
        public int Id { get; set; }
        public string Name { get; set; }

    }

结果: