Unable to bind JSON to ASP.NET model in multi-page message

I am using an ASP.NET MVC3 controller to receive a message from several parts from a WP7 application. The message format is as follows:

    {User Agent stuff}
    Content-Type: multipart/form-data; boundary=8cdb3c15d07d36a

    --8cdb3c15d07d36a
    Content-Disposition: form-data; name="user"
    Content-Type: application/json

    {"UserName":"ashish","Password":"ashish"}

    --8cdb3c15d07d36a--

And my controller looks like this:

    public class User
    {
        public string UserName { get; set;}
        public string Password { get; set; }
    }

    [HttpPost]
    public JsonResult CreateFeed(User user)
    {
    }

What I see is that the User is not connected to json, and the user object is always zero. I tried to create a custom string and manually bind it to the User class using the DataContractJsonSerializer, and it creates and assigns the object, but I'm confused why it doesn't work.

I tried using a non-multi-format post and found that it works with the same json. Any help would be greatly appreciated.

: ASP.NET MVC. Action, multipart/form-data, HTTP http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2, .

+3
1

, ,

. , :

[HttpPost]
public JsonResult CreateFeed(string jsonResponse)
{
    JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
    User user = jsonSerializer.Deserialize<User>(jsonResponse);
}

Content-Disposition , - :

[HttpPost]
public JsonResult CreateFeed()
{
    StreamReader reader = new StreamReader(Request.InputStream);
    string jsonResponse = reader.ReadToEnd();

    JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
    User user = jsonSerializer.Deserialize<User>(jsonResponse);
}

+1

All Articles