【问题标题】:AmbiguousMatchException on controller routes控制器路由上的 AmbiguousMatchException
【发布时间】:2020-11-11 11:42:10
【问题描述】:

我正在尝试在 .NET Core Web API 上构建一个基本的迷你项目,用于执行以下基本操作:GET、POST、PUT、DELETE。

我的WeatherForecastController 中有以下代码触发AmbiguousMatchException

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System.Net;
using System.Web;
using System.Net.Http;


namespace Webdemo.Controllers
{
    [ApiController]
    [Route("api/[controller]/[Action]")]
    public class WeatherForecastController : ControllerBase
    {
     static List<string> names = new List<string>()
         {
            "c","a","b"
         };
        [HttpGet]
        public IEnumerable<string> Get()
        {
            return names;
        }
        [HttpGet]
        public string Get (int id) {
            return names[id];
        }
        [HttpPost]
        public void Post([FromBody]string value)
        {
            names.Add(value);
        }
        [HttpPut]
        public void Put(int id,[FromBody]string value)
        {
            names[id] = value;
        }
       [HttpDelete]
        public void Delete(int id) 
        {
            names.RemoveAt(id);
        }
    }
}

【问题讨论】:

  • 似乎有什么问题?
  • 错误:AmbiguousMatchException:请求匹配多个端点。匹配项: Webdemo.Controllers.WeatherForecastController.Get (Webdemo) Webdemo.Controllers.WeatherForecastController.Get (Webdemo) Webdemo.Controllers.WeatherForecastController.Post (Webdemo) Webdemo.Controllers.WeatherForecastController.Put (Webdemo) Webdemo.Controllers.WeatherForecastController.Delete ( Webdemo) Microsoft.AspNetCore.Routing.Matching.DefaultEndpointSelector.ReportAmbiguity(CandidateState[] CandidateState)

标签: c# list asp.net-core-webapi webapi


【解决方案1】:

错误:AmbiguousMatchException:请求匹配多个端点。

    [HttpGet]
    public IEnumerable<string> Get()
    {
        return names;
    }
    [HttpGet]
    public string Get (int id) {
        return names[id];
    }

问题与上述代码有关,尝试为唯一标识符添加一个占位符变量,更改代码如下:

    // GET: api/<WeatherForecastController>
    [HttpGet]
    public IEnumerable<string> Get()
    {
        return names;
    }
    // GET: api/<WeatherForecastController>/1
    [HttpGet("{id}")] //When this action is invoked, the value of "{id}" in the URL is provided to the method in its id parameter
    public string Get (int id) {
        return names[id];
    }

编辑

关于使用 Asp.net Core API 传递参数的文章:

Tutorial: Create a web API with ASP.NET Core

Parameter Binding in ASP.NET Web API

Multiple GET And POST Methods In ASP.NET Core Web API

【讨论】:

    【解决方案2】:

    您的Get 方法仅使用一组不同的参数匹配同一端点。 您可以通过更改其中一个方法的名称来解决此问题,例如,第二个 Get 方法可能变为 GetSingle,因为它似乎通过其 idnames 列表中获取单个条目。

    你可以这样做:

    [HttpGet]
    public string GetSingle (int id) 
    {
        return names[id];
    }
    

    【讨论】:

      猜你喜欢
      • 2021-01-05
      • 2013-12-14
      • 2016-03-16
      • 2016-07-12
      • 2012-04-14
      • 2023-03-22
      • 2014-07-20
      • 1970-01-01
      相关资源
      最近更新 更多