Question

I'm using AttributeRouting library to make rooting easier. I've a method marked with POST attribute and gets a string parameter as below.

[POST("Create")]
public ActionResult Create(string data)
{
    JavaScriptResult result = new JavaScriptResult();

    User user = JsonConvert.DeserializeObject<User>(data);
    BLResult bLResult = AccountingBL.CreateAccount(user);
    result.Script = JsonConvert.SerializeObject(bLResult);

    return result;
}

I use Fiddler to simulate post request with following headers and request body.

Headers:

User-Agent: Fiddler
Host: localhost:2253
Content-Type: application/json
Content-Length: 67

Request Body:

{"Email":"foo@foo.com","Password":"123456","Name":"Halil İbrahim"}

When i track the request, i see that data is null. Where I miss the point?

Was it helpful?

Solution

You should make use of the ModelBinding features of ASP.NET MVC to get the User instance directly

Restructure your Action to

[POST("Create")]
public ActionResult Create(User user)
{
    BLResult bLResult = AccountingBL.CreateAccount(user);

    result.Script = JsonConvert.SerializeObject(bLResult);

    return result;
}

where User is of the form . Make sure that User has a no argument constructor

public class User {

    public string Name {get;set;}

    public string Password {get;set;}

    public string Email {get;set;}

}

And Send the data to the POST as

var user = {"Email":"foo@foo.com","Password":"123456","Name":"Halil İbrahim"}

 $.ajax({
   .... other details
   data : user
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top