How to retrieve HttpPost parameters in c#

13.6k views Asked by At

So I know that this works:

[HttpPost]
public string functionthatiuse()
{
    string id = "";//does nothing
    return relevantinfo;
}

Then I use this Chrome POST extension shown below and I have a break point in the function which is how I know it reaches it. It's literally an empty post request basically.

dsdf

But when I try to post with parameters I'm having trouble. Ideally I want to do something like this:

[HttpPost]
public string functionthatiuse(string idx)
{
    string id = ""; //does nothing and is different from idx
    return relevantData;
}

but when I try to use it I get an error back. I'm pretty sure it's because I'm not formatting the content body correctly and I've tried putting other stuff in the content body, but nothing has really worked. Does anyone know how I can send POST parameters to this function using this extension? The format of what I'm doing in code should be basically the same (part of a requirement).

Edit:

Here's a picture of the error:

enter image description here

1

There are 1 answers

0
GDub On BEST ANSWER

According to microsoft here: http://www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api

You have to add "[FromBody]" in the parameter list. You can only have one of these types of parameters.

Also in the chrome post extension under headers you need to enter:

Name: Content-Type Value: application/json