Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core WebAPI 2 PUT method name

I have been struggling for a while and cannot seem to get this to work.

I have a Controller, say "Teacher".

I want a PUT action with a different name, but accepts [FromBody] a complex DTO.

How can I invoke it? Everything I tried is giving me a 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();
    }
}

I have tried (and cried!):

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

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

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

I always get a 404.

The simple Put works i.e:

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

But I want to use a different action name.

Thoughts?

like image 355
Ahmed ilyas Avatar asked Oct 23 '25 20:10

Ahmed ilyas


1 Answers

The route template was not matching the URL being called

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

Reference Routing to Controller Actions

like image 67
Nkosi Avatar answered Oct 25 '25 11:10

Nkosi