Ralph
Ralph

Reputation: 897

Get Js FormData() In Web Api

Is there a trick to getting formdata from a post request in web api? I can't seem to be able to use namevaluecollection or formdatacollection when submitting a form on the frontend using js FormData(). I have checked the http request to verify the form data is being passed to the server.

JS Form Submit

    var formData = new FormData();
    formData.append('Email', '[email protected]');
    formData.append('Password', '123');
    // submit data using xmlhttprequest

Controller Web Api

public IHttpActionResult CheckUser(FormDataCollection FormData)
{
     //formdatacollection and namevaluecollection don't seem to return key based values
}

Upvotes: 0

Views: 535

Answers (1)

Oleg Safarov
Oleg Safarov

Reputation: 2345

Actually, in web api controllers you should check the property Request to get all information you post from your page. Next you can get your data by using appropriate method: Request.Content.ReadAsByteArrayAsync or Request.Content.ReadAsFormDataAsync (it's probably your case) or Request.Content.ReadAsStreamAsync, etc. It depends on the sent data format.

Or you can always create a class with the respective fields and send it in JSON to get it another way:

public IHttpActionResult CheckUser(AccountData data)
{

}

And your class should look like:

public class AccountData {    
   public String Email { get; set; }    
   public String Password { get; set; }
}

Upvotes: 2

Related Questions