简体   繁体   English

Asp.Net Web Api Multipost参数

[英]Asp.Net Web Api Multipost Parameter

So, i'm trying to pass multiple parameters from fiddler to my web api, using FormDataCollection.ReadAsNameValueCollection() . 所以,我正在尝试使用FormDataCollection.ReadAsNameValueCollection()将多个参数从fiddler传递到我的web api。 Problem is everytime is send my data, formData comes back as null. 问题是每次都发送我的数据, formData返回为null。 I'm not sure what I'm doing wrong here. 我不确定我在这里做错了什么。 I've tried decorating formData with a [FromBody] attribute. 我尝试用[FromBody]属性装饰formData Also registered JsonMediaTypeFormatter() in the global.asax class. 还在global.asax类中注册了JsonMediaTypeFormatter()

Any help would be much appreciated. 任何帮助将非常感激。

Please see code below: 请参阅以下代码:

[HttpPost]
public HttpResponseMessage PostAccount([FromBody]FormDataCollection formData)
{
    if (formData != null)
    {
        var nValueCol = formData.ReadAsNameValueCollection();

        var account = new Account()
        {
            Email = nValueCol["email"],
            Password = nValueCol["password"],
            AgreedToTerms = Convert.ToBoolean(nValueCol["agreesToTerms"]),
            //LocationAccountCreated = DbGeography.FromText(nValueCol["userLocation"])
        };

        var userProfile = new UserProfile()
        {
            FirstName = nValueCol["firstName"],
            LastName = nValueCol["lastName"],
            DateOfBirth = Convert.ToDateTime(nValueCol["dateOfBirth"])
        };

        var newAcc = _accountService.CreateAccount(account.Email, userProfile.FirstName, userProfile.LastName,
                                                    userProfile.DateOfBirth, account.Email, account.AgreedToTerms,
                                                    account.LocationAccountCreated);
        var response = Request.CreateResponse(HttpStatusCode.Created);

        return response;
    }
    else
        return Request.CreateResponse(HttpStatusCode.NotAcceptable);
}

Sample request: 样品申请:

提琴手发布请求

FormDataCollection is normally associated with application/x-www-form-urlencoded media type. FormDataCollection通常与application/x-www-form-urlencoded媒体类型相关联。

Your screen shot shows you are trying to send json data. 您的屏幕截图显示您正在尝试发送json数据。 If you don't have a concrete data type for your data and you want to send it as Json you can use an IDictionary<string,string> which will be mapped by the model binder successfully. 如果您没有数据的具体数据类型,并且想要将其作为Json发送,则可以使用IDictionary<string,string> ,它将成功地由模型绑定器映射。

You action will look something like... 你的行动看起来像......

[HttpPost]
public HttpResponseMessage PostAccount([FromBody]IDictionary<string, string> formData) {
    if (formData != null) {
        var nValueCol = formData;
        //...other code removed for brevity, but can basically stay the same
        var response = Request.CreateResponse(HttpStatusCode.Created);
        return response;
    } else
        return Request.CreateResponse(HttpStatusCode.NotAcceptable);
}

Based on your code and the information from your fiddler screen shot, a TestController was created, a request was tested with fiddler like... 基于你的代码和你的小提琴屏幕截图中的信息,创建了一个TestController,一个请求用fiddler测试,如...

POST http://localhost:35979/api/account/create HTTP/1.1
User-Agent: Fiddler
Host: localhost:35979
Content-Type: application/json
Content-Length: 76

{"email":"myemail@email.com",
"firstname":"myFName",
"lastName":"myLName"}

...and the formData was populate with the 3 fields and their data. ...并且formData填充了3个字段及其数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM