1

I'm currently submitting a json request to the following .NET MVC action

 [AcceptVerbs(HttpVerbs.Post)]
    public JsonResult Signup(FormCollection collection)
    {

However the collection is empty for some reason. I know that the data is being sent because if I change it to

[AcceptVerbs(HttpVerbs.Post)]
    public JsonResult Signup(String username, String email, String password)
    {

I'm using form collection because it allows me to have optional parameters for the method. In the future I might not have username i might have name (for example) but I can't have the old url break

The request is coming from an iOS app in the form of a NSMutableURLRequest. Am I missing something or do I have to use the second option and create a different method for when I add new parameters/ create a separate version of the .NET MVC app for when I release new app updates.

So yeah its because I'm posting the data like this

NSMutableDictionary *sendData = [[NSMutableDictionary alloc] init];

        [sendData setValue:self.usernameTxt.text forKey:@"username"];
        [sendData setValue:self.emailTxt.text forKey:@"email"];
        [sendData setValue:self.deviceToken forKey:@"device_token"];

        //verify account details and retrieve the API Key
        responseData = [NSMutableData data];    

        NSString *stringUrl = kAppRequestUrl;

        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[stringUrl stringByAppendingString:@"/SomePath"]]];
        [request setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
        [request setHTTPMethod:@"POST"];

        [request setHTTPBody: [sendData JSONData]];
tereško
  • 58,060
  • 25
  • 98
  • 150
Luke
  • 3,375
  • 2
  • 22
  • 22

1 Answers1

0

FormCollection means a collection of your Form elements. Do not expect to accept JSON values in that. I would ise your second Option (parameters) with Optional parameters.

[HttpPost]
public JsonResult Signup(String username="",string name="", String email, String password)
{
   //do stuff
}
Shyju
  • 214,206
  • 104
  • 411
  • 497
  • Yeah so I ended up just having all my parameters optional. Otherwise I have to do something like http://stackoverflow.com/questions/1571336/sending-post-data-from-iphone-over-ssl-https – Luke Jul 27 '12 at 08:59