0

In a controller, I have a method like this:

public class FooController : ApiController
{
    [HttpPost]
    public HttpResponseMessage ApplySomething(int id)
    {
        ...
    }
}

When I do a POST request like .../Foo/ApplySomething/ and passing id=1 as POST values, I get the error:

{
    Message: "No HTTP resource was found that matches the request URI '.../Foo/ApplySomething/'."
    MessageDetail: "No action was found on the controller 'Foo' that matches the name 'ApplySomething'."
}

But when I change the URL to have the ID (like .../Foo/ApplySomething/1) it works, but getting the value from the URL, not from POST values.

What I'm doing wrong?

2
  • Does /Foo/ApplySomething?id=1 work? Commented Jun 5, 2014 at 19:42
  • Yes, /Foo/ApplySomething?id=1 and /Foo/ApplySomething/1 works the same way. Commented Jun 5, 2014 at 19:45

3 Answers 3

1

By default, Web API uses the following rules to bind parameters:

  • If the parameter is a “simple” type, Web API tries to get the value from the URI. Simple types include the .NET primitive types (int, bool, double, and so forth), plus TimeSpan, DateTime, Guid, decimal, and string, plus any type with a type converter that can convert from a string. (More about type converters later.)
  • For complex types, Web API tries to read the value from the message body, using a media-type formatter.

Given those rules, if you want to bind the parameter from the POST body simply add a [FromBody] attribute in front of the type:

public HttpResponseMessage ApplySomething([FromBody] int id) { ... }

For more information please see the documentation.

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

3 Comments

It seems to me that when I use FromBody the whole content is bound to a single parameter, instead of using 'id=1&anotherParam=value' format, am I right?
Your question didn't mention any other parameters, so it depends on what you need to do. If you require multiple parameters I would just build a complex type (e.g. some model class) and have all your needed parameters as properties. By default the framework will bind to that type and you'll be good to go. There's nothing stopping you from using both ([FromBody] int id, YourModel model) as the method signature though.
Yes, exactly, if you want to parse it into separate fields you can use other type for field 'FormDataCollection' instead of int
0

Try this :

public class FooController : ApiController
{
    [HttpGet]
    public HttpResponseMessage ApplySomething(int? id=0)
    {
        ...
       return View();    
    }
    [HttpPost]
    public HttpResponseMessage ApplySomething(FormCollection Form,int? id=0)
    {
        ...
       return View();    
    }
}

Now Try this url .../Foo/ApplySomething?id=1

Hopefully it works...!

Comments

0

mention the httpget method with same action or check you routeconfig file.

you can also refer following solutions:

solution 1: No HTTP resource was found that matches the request URI in Web API

solution 2: http://forums.asp.net/t/1916587.aspx?WebAPI+No+HTTP+resource+was+found+that+matches+the+request+URI

solution 3: http://our.umbraco.org/forum/developers/extending-umbraco/46086-UmbracoApiController-No-HTTP-resource-was-found-that-matches-the-request-URI

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.