1

I am calling a MVC Api Controller using PostAsJsonAsync.

If I want to pass a Complex type like a Class instance, it Works fine. But I need to pass a Simple type as string, or int like

HttpResponseMessage response = await
 client.PostAsJsonAsync("http://localhost:62536/api/Controller/Method", "HELLO");

Got an error 404 Method not found.

My WebApiController looks like this

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

My Api Controller Method looks like this.

public void Method(string id){}

If a try another Mehtod with Int type it does not work either..

 HttpResponseMessage response = await
     client.PostAsJsonAsync("http://localhost:62536/api/Controller/MethodA", 123);

public void MethodA(int id){}

If I assign that value to a Complex Type like a Class.. it Works fine.

PostAsJsonAsync only Works for complex Type? How can it make it work?

Diego
  • 2,238
  • 4
  • 31
  • 68
  • Possible duplicate of [Why do we have to specify FromBody and FromUri?](https://stackoverflow.com/questions/24625303/why-do-we-have-to-specify-frombody-and-fromuri) –  Aug 11 '17 at 00:50

2 Answers2

4
public void Method([FromBody] string id){}
Qaiser Mehmood
  • 202
  • 2
  • 10
-1

Looks like parse issue. Create string variable first then pass to method.

string x = (string)value;
Qaiser Mehmood
  • 202
  • 2
  • 10