1

I have an AngularJS app that has to post data to a 3rd party URL. To get around the CORS browser issue, I am posting the data to my .net controller which will then post it to the 3rd party URL. In my javascript file, I've built the query string I want to pass to the 3rd party URL. Here is my code that posts to my .net controller:

var parms = "name=john&phone=1234567890&sex=male";
return $http({
                    url: '/UserForm/PostData/',
                    method: "POST",
                    data: parms
                });

My c# controller:

[HttpPost]
public string PostData(String parms)
{
    return "";
}

the parms variable in PostData is null. What do I need to do to pass my querystring string to my controller?

2
  • is this MVC or Web API? Commented Apr 29, 2016 at 1:56
  • @user1024941 - did any of the answers solve your problem. If so mark them as accepted. Commented May 10, 2016 at 12:44

3 Answers 3

2

Can you try this? First - why don't you just define all parameters inside the controller post request definition? Second - I guess you have an error in your URL, you don't need the slash after "/UserForm/PostData" (If you don't use some custom routing in your MVC of course)

$http.post("/UserForm/PostData", { name: "john", phone: "1234567890", sex: "male" }).then(blahblahal....;

+

[HttpPost]
public string PostData(String name, String phone, String sex)
{
    return "";
}
Sign up to request clarification or add additional context in comments.

Comments

1

If this is for a WebApi controller, instead of trying to read parms as a parameter passed to the action, use ReadAsString to read the posted data. In your example the result should be the same value in your parms variable.

[HttpPost]
public IHttpActionResult PostData()
{
    var parms = Request.Content.ReadAsStringAsync().Result;
    // Submit parms to third-party api.
    return Ok();
}

This task is more appropriate for WebApi, but if you need to use an MVC controller, you can read what was posted by reading Request.InputStream:

public ActionResult PostData()
{
    string parms;
    using (var sr = new System.IO.StreamReader(Request.InputStream))
    {
         parms = sr.ReadToEnd();
    }
    // Submit parms to third-party api.
    return new EmptyResult();
}

3 Comments

that isn't very testable.
I get the error "system.web.httprequestbase' does not contain a definition for 'content'".
Seems like you are using MVC instead of WebApi. See changed answer.
0

You can try this.

[HttpPost]
public string PostData(System.Net.Http.Formatting.FormDataCollection form)
{
    string name=form.get("name");
    string phone=form.get("phone");
    string sex=form.get("sex");
}

Or else you can try this solution too, you just need to add couple of lines.

public class PersonData
{
   public string name { get; set; }
   public string phone { get; set; }
   public string sex { get; set; }
}

[HttpPost]
public string PostData([FromBody]PersonData form)
{
   string name=form.name;
   string phone=form.phone
   string sex=form.sex;
}

2 Comments

This gives the error the "type or namespace name 'Formatting' does not exist in the namespace 'system.net.http'" I have using System.Net.Http; at the top of my class.
Have you referenced the System.Net.Http assembly in your project?

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.