這是我的控制器是什麼樣子:爲什麼我的屬性路由不起作用?
[Route("api/[controller]")]
[Produces("application/json")]
public class ClientsController : Controller
{
private readonly IDataService _clients;
public ClientsController(IDataService dataService)
{
_clients = dataService;
}
[HttpPost]
public int Post([Bind("GivenName,FamilyName,GenderId,DateOfBirth,Id")] Client model)
{
// NB Implement.
return 0;
}
[HttpGet("api/Client/Get")]
[Produces(typeof(IEnumerable<Client>))]
public async Task<IActionResult> Get()
{
var clients = await _clients.ReadAsync();
return Ok(clients);
}
[HttpGet("api/Client/Get/{id:int}")]
[Produces(typeof(Client))]
public async Task<IActionResult> Get(int id)
{
var client = await _clients.ReadAsync(id);
if (client == null)
{
return NotFound();
}
return Ok(client);
}
[HttpGet("api/Client/Put")]
public void Put(int id, [FromBody]string value)
{
}
[HttpGet("api/Client/Delete/{id:int}")]
public void Delete(int id)
{
}
}
然而,當我請求URL /api/Clients/Get
,如下所示:
json = await Client.GetStringAsync("api/Clients/Get");
我得到以下異常:
AmbiguousActionException:多個操作匹配。下面 行動匹配的路由數據,並已全部約束滿足:
Assessment.Web.Controllers.ClientsController.Index(Assessment.Web) Assessment.Web.Controllers.ClientsController.Details(Assessment.Web) Assessment.Web .Controllers.ClientsController.Create(Assessment.Web)
Client
是HttpClient
。請注意,儘管使用相同的名稱id
,但沒有GET操作與路線數據匹配。
這裏有什麼問題?
MVC-5? - 看起來像asp.net-core-mvc –
@StephenMuecke它是核心,我的錯誤。我經常混淆了MVC 5和6. – ProfK
這是「ClientsController」的整個界面嗎?異常消息中的3個操作方法不在代碼示例中。如果全部都發布了,我們需要查看其餘的路由配置,以確定哪條路由匹配'api/Clients/Get'的請求。 – NightOwl888