我目前正在向下面的.NET MVC操作提交一个json请求
[AcceptVerbs(HttpVerbs.Post)]
public JsonResult Signup(FormCollection collection)
{但是,由于某些原因,集合是空的。我知道数据正在发送,因为如果我将其更改为
[AcceptVerbs(HttpVerbs.Post)]
public JsonResult Signup(String username, String email, String password)
{我使用表单集合,因为它允许我为方法提供可选的参数。将来我可能没有用户名,我可能有名字(例如),但我不能有旧的url断点
该请求来自NSMutableURLRequest形式的iOS应用程序。我是否遗漏了什么,或者我是否必须使用第二个选项,并在添加新参数时创建不同的方法/在发布新应用程序更新时为.NET MVC应用程序创建单独的版本。
所以,是的,这是因为我像这样发布数据
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]];发布于 2012-07-27 02:21:38
FormCollection是指Form元素的集合。不要期望在其中接受JSON值。我将使用可选参数简化您的第二个选项(参数)。
[HttpPost]
public JsonResult Signup(String username="",string name="", String email, String password)
{
//do stuff
}https://stackoverflow.com/questions/11675547
复制相似问题