0

I have REST web services POST method:

public string Post([FromBody]string value1 )  
{
    return "a";
}

I have placed breakpoint inside of it. I have sent POST:

localhost/RESTServer/api/Person/?value1=1

I get null in value1 inside of Post method. What I do wrong?

vico
  • 17,051
  • 45
  • 159
  • 315

2 Answers2

5
public string Post([FromBody]string value1 )  

should be

public string Post([QueryString]string value1 )  

(that is ... because your request is a GET and has no body... pure guess)

DarkSquirrel42
  • 10,167
  • 3
  • 20
  • 31
  • 1
    Well, the request *could* be a `POST`, the question didn't specify. But regardless of the request type, the value is still in the query string. – David Mar 31 '17 at 12:35
  • sure... but when i read the question, something tells me that it is very likely a GET ;) ... but you are right... it might be a POST – DarkSquirrel42 Mar 31 '17 at 12:37
  • I'm trying to use POST. But 'FromBody' was generated by Visual Studio 2015 and if I place 'QueryString' I get error 'The type or namespace name 'QueryStringAttribute' could not be found' – vico Mar 31 '17 at 15:46
  • a new using directive might help you... `using System.Web.ModelBinding;` – DarkSquirrel42 Mar 31 '17 at 15:51
  • This helped with QueryString. But now I have '405 Method Not Allowed' in client side. – vico Mar 31 '17 at 16:20
  • different problem -> different question – DarkSquirrel42 Apr 03 '17 at 09:08
0

I see you are just learning WebApi I recommend you this page: http://csharp-video-tutorials.blogspot.mx/2016/09/aspnet-web-api-tutorial-for-beginners.html by far the best tutorial out there.

Also about your problem : enter image description here

Your post request should look something like this, if you intend to use Post

Angel Silva
  • 365
  • 2
  • 5
  • 16