gpt4 book ai didi

asp.net-mvc - 为什么我的属性路由不起作用?

转载 作者:行者123 更新时间:2023-12-03 14:11:36 24 4
gpt4 key购买 nike

这是我的 Controller 的样子:

[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: Multiple actions matched. The following actions matched route data and had all constraints satisfied:

Assessment.Web.Controllers.ClientsController.Index (Assessment.Web) Assessment.Web.Controllers.ClientsController.Details (Assessment.Web) Assessment.Web.Controllers.ClientsController.Create (Assessment.Web)


ClientHttpClient .请注意,没有与路由数据匹配的 GET 操作,尽管名称相同, id .

这里可能有什么问题?

最佳答案

您使用的属性错误。

您在 Controller 上有一条路线

[Route("api/[controller]")]

这将映射到 api/Clients并将其作为 Controller 中任何操作路由的前缀。

所以这意味着
[HttpGet("api/Client/Get")] // Matches GET api/Clients/api/Client/Get
[Produces(typeof(IEnumerable<Client>))]
public async Task<IActionResult> Get()
{
var clients = await _clients.ReadAsync();
return Ok(clients);
}

将 GET 匹配到 api/Clients/api/Client/Get因为 Controller 上的路由前缀。

引用 Routing to Controller Actions

您需要相应地更新操作的属性路由
[Route("api/[controller]")]
[Produces("application/json")]
public class ClientsController : Controller {
private readonly IDataService _clients;

public ClientsController(IDataService dataService)
{
_clients = dataService;
}

[HttpPost] //Matches POST api/Clients
public int Post([Bind("GivenName,FamilyName,GenderId,DateOfBirth,Id")] Client model) {
// NB Implement.
return 0;
}

[HttpGet("Get")] //Matches GET api/Clients/Get
[Produces(typeof(IEnumerable<Client>))]
public async Task<IActionResult> Get() {
//...code removed for brevity
}

[HttpGet("Get/{id:int}")] //Matches GET api/Clients/Get/5
[Produces(typeof(Client))]
public async Task<IActionResult> Get(int id) {
//...code removed for brevity
}

[HttpGet("Put")] //Matches PUT api/Clients/Put
public void Put(int id, [FromBody]string value) {
//...code removed for brevity
}

[HttpGet("Delete/{id:int}")] //Matches GET api/Clients/Delete/5
public void Delete(int id) {
}
}

删除操作实际上应该重构为 HTTP DELETE 并且应该返回 IActionResult
[HttpDelete("Delete/{id:int}")] //Matches DELETE api/Clients/Delete/5
public IActionResult Delete(int id) {
//...code removed for brevity
}

关于asp.net-mvc - 为什么我的属性路由不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46997593/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com