MVC3 comes out of the box with JsonValueProviderFactory() which is very handy for binding incoming JSON to a model. Unfortunately, I can't figure out how to setup model contracts with names that differ from the incoming JSON. For example:
[DataContract(Name = "session")]
public class FacebookSession
{
[DataMember(Name = "access_token")]
public string AccessToken { get; set; }
[DataMember(Name = "expires")]
public int? Expires { get; set; }
[DataMember(Name = "secret")]
public string Secret { get; set; }
[DataMember(Name = "session_key")]
public string Sessionkey { get; set; }
[DataMember(Name = "sig")]
public string Signature { get; set; }
[DataMember(Name = "uid")]
public string UserId { get; set; }
}
when passing in a json object representing the facebook session, the properties secret and expires bind properly, but the rest do not because the property name is different than the json key name. I would expect that the datacontract serializer would try and bind to the name provided in the attribute, but that doesn't appear to be the case. Does anyone have any workaround suggestions?
Edit
An example of how I would use this model:
public ActionResult Log(int? custId, FacebookSession response)
{
ViewBag.Id = response.UserId;
return View();
}
ModelBinders.Binders.DefaultBinder = new SmartModelBinder(new [] {new NewtonsoftJsonModelBinder()});
– Joaquin