【问题标题】:ASP.NET Core WebAPI 2 PUT method nameASP.NET Core WebAPI 2 PUT 方法名称
【发布时间】:2018-08-10 18:58:02
【问题描述】:

我已经苦苦挣扎了一段时间,似乎无法让它发挥作用。

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

我想要一个具有不同名称的 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();
}

但我想使用不同的动作名称。

想法?

【问题讨论】:

    标签: c# asp.net-core-webapi asp.net-core-routing


    【解决方案1】:

    路由模板与被调用的 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

    【讨论】:

    • @Ahmedilyas 我刚开始的时候也是这样。把它归结为我努力学习,这样你就不必这样做了。大声笑
    猜你喜欢
    • 2015-05-28
    • 1970-01-01
    • 1970-01-01
    • 2019-09-22
    • 2017-04-04
    • 2021-08-20
    • 2017-10-30
    • 1970-01-01
    • 2021-02-15
    相关资源
    最近更新 更多