简体   繁体   中英

Asp.Net Web Api Multipost Parameter

So, i'm trying to pass multiple parameters from fiddler to my web api, using FormDataCollection.ReadAsNameValueCollection() . Problem is everytime is send my data, formData comes back as null. I'm not sure what I'm doing wrong here. I've tried decorating formData with a [FromBody] attribute. Also registered JsonMediaTypeFormatter() in the global.asax class.

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.

Your screen shot shows you are trying to send json data. 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.

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...

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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