2

Route Config

config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
);

POST-method

[HttpPost]
public Probabilities Post(string word, string userId) {
    Request request = new Request();
    request.Identify(word, userId);
    Probabilities probabilities = probabilitiesFactory.GetBy(request.ProbabilitiesId, "Id");
    return probabilities;
}

Im trying to post some data, shown on the screenshot

Postman

And im getting an error

"No action was found on the controller 'Identification' that matches the request."

What is happening? How to post 2 simple strings and get the result

3
  • What is the exact name of your controller? The class that contain your action. Commented Aug 17, 2017 at 14:31
  • @codenotfound its name is IdentificationController Commented Aug 17, 2017 at 14:32
  • 1
    What the Headers tab contains in your postman UI ? It should work. MAybe you need to change the "Text" tab to "Json" :) Commented Aug 17, 2017 at 14:33

2 Answers 2

1

While not mandatory, you should follow good practice and encapsulate the payload into a model.

public class MyModel {
    public sting word { get; set; }
    public sting userId { get; set; }
}

You can then use the FromBody parameter attribute to bind the model to the data sent.

Finally addressing the resource not found issue, ensure that the controller is following the proper convention given that the OP is configured with convention-based routing.

public class IdentificationController : ApiController {

    [HttpPost]
    public Probabilities Post([FromBody] MyModel model) {
        string word = model.word;
        string userId = model.userId;
        Request request = new Request();
        request.Identify(word, userId);
        Probabilities probabilities = probabilitiesFactory.GetBy(request.ProbabilitiesId, "Id");
        return probabilities;
    }
}

Reference: Parameter Binding in ASP.NET Web API

Reference: Routing in ASP.NET Web API

Sign up to request clarification or add additional context in comments.

Comments

1

There are 2 different solutions for this problem. First: Send word and userId as QueryStrings.. here's an example:

http://localhost/api/identification?word=hey&&userId=12

this will work in your case so you won't need to change anything in the code.

And Finally my favorite solution: Create a model class like that:

public class IdentificationModel{
    public string word { get; set; }
    public string userId { get; set; }
}

then require it as a parameter object in you method like that:

[HttpPost]
public Probabilities Post(IdentificationModel requestModel) {
    Request request = new Request();
    request.Identify(requestModel.word, requestModel.userId);
    Probabilities probabilities = probabilitiesFactory.GetBy(request.ProbabilitiesId, "Id");
    return probabilities;
}

The last one does not require adding [FromBody] attribute since any class object is an api method will automatically be waited as a Request Body Object.

2 Comments

I found that the variables on the model class only got set if they had a public get and set method.
@AdamR.Grey Yeah I guess you were right I forgot it, I edited my answer. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.