Click here to Skip to main content
15,888,069 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a WebAPI project which was generated by the Visual Studio 2015 wizard. In it, I have added a controller that contains a GET handler:
public Task<string> Get (int id)
{
    using (var wb = new WebClient())
    {
        var data = new NameValueCollection();
        data["username"] = "myUser";
        data["password"] = "myPassword";

        var response = wb.UploadValues(<project URL, ends with a value for the id  
            parm>, "POST", data);
    }

    return null;
}

And I have the following method:

[HttpPost]
public NameValueCollection Post(int id, [FromBody] NameValueCollection data)
{
    return data;
}

I find that although I have deliberately hard-coded the data in the GET handler, when the POST method gets invoked, the data parameter always has a .Count of 0. Upon return to the GET, it has a .Count of 2, exactly as it started out. I have tried a dozen different methods of invoking the POST, taken from various articles online. In every case, the behavior is the same: the POST method receives no data. The id parameter always has the correct value passed in to the GET method.

Does anyone have a clue about what might be going wrong?

What I have tried:

I have tried a dozen different methods from articles found on the web.
Posted
Updated 18-Sep-16 5:50am

1 solution

First, your REALLY need to learn basic C# before you try this. Why?

Because your Get method is hard coded to return nothing at all. See that return null at the bottom of the method. Yeah.

Next, your Get method says it's returning a Task<string></string>, but it's not doing anything that resembles setting up and returning a Task that returns a string.

I wish I could tell you how to "fix" this, but I have no idea what you're trying to do. You've got a Get method that isn't Getting and returning anything. It's actually Putting values into some other web URL, whatever that is. Your code is schizophrenic at best.
 
Share this answer
 
Comments
rrotstein 18-Sep-16 12:00pm    
This code is meant as a first attempt at POSTing data. I realize that it does not make much sense as a working example; my intention was to just get the POST method invoked and then build up the code.

I had tried several approaches taken from the following articles:

http://stackoverflow.com/questions/4015324/http-request-with-post
http://stackoverflow.com/questions/21051743/submit-post-request-from-codebehind-in-asp-net
http://stackoverflow.com/questions/5401501/how-to-post-data-to-specific-url-using-webclient-in-c-sharp

None of them worked.

Perhaps you could could just tell me what is a valid way to invoke a POST method directly from a browser?
Dave Kreskowiak 18-Sep-16 18:22pm    
There are millions of examples of this on the web. All you have to do is Google for "javascript post request".

Of course, you have to have server-side code THAT IS CORRECT in order for the Post to do anything.

Here, have a shot at http://stackoverflow.com/questions/26212987/mvc-how-to-call-controller-post-method

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900