【发布时间】: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