Freelance1382
Freelance1382

Reputation: 35

WebAPI async Task<IHttpActionResult> passing model

This is my controller

public class TutorController : ApiController
{

    [Route("CreateTutor")]
    public async Task<IHttpActionResult> CreateTutor(TutorModel model)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }            

        return Ok();
    }
}

and I am using Fiddler to connect to it

POST http://localhost:12110/api/Tutor/CreateTutor

I have set raw and application/application

In Body I have

{
  "Name": "Test"
}

But I get this error { "Message": "No HTTP resource was found that matches the request URI 'http://localhost:12110/api/Tutor/CreateTutor'.", "MessageDetail": "No action was found on the controller 'Tutor' that matches the request." }

Any idea what I am doing wrong?

Upvotes: 0

Views: 839

Answers (1)

Hamarict
Hamarict

Reputation: 143

Every method in an API controller must have an attribute to determine it

You are missing the action attribute in your method such as the eg. below

[Route("api/[controller]")]
[ApiController]
public class TutorController : ApiController
{

    [HttpPost]
    [Route("CreateTutor")]
    public async Task<IHttpActionResult> CreateTutor(TutorModel model)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }            

        return Ok();
    }
}

Read more in the MS docs: https://learn.microsoft.com/en-US/aspnet/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2#http-methods

Upvotes: 1

Related Questions