编程语言
首页 > 编程语言> > C#-ASP.NET Core WebAPI 2 PUT方法名称

C#-ASP.NET Core WebAPI 2 PUT方法名称

作者:互联网

我已经苦苦挣扎了一段时间,似乎无法解决这个问题.

我有一个控制器,说“老师”.

我想要一个具有不同名称的PUT操作,但是接受[FromBody]一个复杂的DTO.

如何调用?我尝试的一切都给了我404.

[Produces("application/json")]
[Route("api/Teacher")]
public class TeacherController : Controller
{
    private readonly ITeacherService _teacherService;

    public TeacherController(ITeacherService teacherService)
    {
        this._teacherService = teacherService;
    }

    [HttpPut("UpdateTeacherForInterview")]
    public IActionResult PutTeacherForInterview(int id, [FromBody]UpdateInterviewModel model)
    {
        return Ok();
    }
}

我尝试过(哭了!):

PUT /api/Teacher/1 (and complex object)

PUT /api/Teacher/UpdateTeacherForInterview/1 (and complex object)

PUT /api/Teacher/PutTeacherForInterview/1 (and complex object)

我总是得到404.

简单的Put可以工作,即:

[HttpPut]
public IActionResult Put(int id, [FromBody]string value)
{
    return Ok();
}

但我想使用其他动作名称.

有什么想法吗?

解决方法:

路由模板与被调用的URL不匹配

//Matches PUT api/Teacher/UpdateTeacherForInterview/1
[HttpPut("UpdateTeacherForInterview/{id:int}")]
public IActionResult PutTeacherForInterview(int id, [FromBody]UpdateInterviewModel model) {
    return Ok();
}

参考Routing to Controller Actions

标签:asp-net-core-webapi,asp-net-core-routing,c
来源: https://codeday.me/bug/20191109/2012575.html