Pro*_*ofK 3 asp.net-mvc attributerouting dotnet-httpclient asp.net-core-mvc
这是我的控制器的外观:
[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)
{
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我请求URL时/api/Clients/Get,如下所示:
json = await Client.GetStringAsync("api/Clients/Get");
Run Code Online (Sandbox Code Playgroud)
我得到以下异常:
AmbiguousActionException:多个动作匹配。以下操作与路线数据匹配,并且满足所有约束:
Assessment.Web.Controllers.ClientsController.Index(Assessment.Web)Assessment.Web.Controllers.ClientsController.Details(Assessment.Web)Assessment.Web.Controllers.ClientsController.Create(Assessment.Web)
Client是一个HttpClient。请注意,尽管名称相同,但没有GET操作与路线数据匹配id。
这有什么问题吗?
您使用的属性错误。
您在控制器上有一条路线
[Route("api/[controller]")]
Run Code Online (Sandbox Code Playgroud)
它将映射到api/Clients控制器中的任何动作路由并在其之前加上前缀。
所以这意味着
[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);
}
Run Code Online (Sandbox Code Playgroud)
api/Clients/api/Client/Get由于控制器上的路由前缀而将GET匹配到。
引用路由到控制器操作
您需要相应地更新操作上的属性路由
[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) {
}
}
Run Code Online (Sandbox Code Playgroud)
实际上,delete动作应重构为HTTP DELETE并应返回 IActionResult
[HttpDelete("Delete/{id:int}")] //Matches DELETE api/Clients/Delete/5
public IActionResult Delete(int id) {
//...code removed for brevity
}
Run Code Online (Sandbox Code Playgroud)
小智 0
我认为路由中有一个拼写错误,它应该是 Client s末尾带有 (s) 而不是 Client 。
[HttpGet("api/Clients/Get")]
Run Code Online (Sandbox Code Playgroud)
代替
[HttpGet("api/Client/Get")]
Run Code Online (Sandbox Code Playgroud)
或者将对端点的调用更改为:
json = await Client.GetStringAsync("api/Client/Get");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2751 次 |
| 最近记录: |