0
votes

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?

1
What does your Startup.cs file look like? you may not be using default route of "api/controller/action". You could also try placing [HttpPost] attribute on your controller method as well. - sspaniel
When you apply the Route attribute you are overriding the default route in the WebApiConfig file. Try accessing your controller with the following...it will probably work, thought it's probably not what you want: localhost:12110/CreateTutor - Kenneth K.

1 Answers

1
votes

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://docs.microsoft.com/en-US/aspnet/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2#http-methods