MVC3开箱即用JsonValueProviderFactory(),它非常方便将传入的
JSON绑定到模型.不幸的是,我无法弄清楚如何设置名称与传入JSON不同的模型合同.例如:
[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; } }
传入表示facebook会话的json对象时,属性secret和expires正确绑定,但其余部分没有,因为属性名称与json键名称不同.我希望datacontract序列化程序可以尝试绑定到属性中提供的名称,但似乎并非如此.有没有人有任何解决方法的建议?
编辑
我将如何使用此模型的示例:
public ActionResult Log(int? custId,FacebookSession response) { ViewBag.Id = response.UserId; return View(); }
解决方法
我最终使用gt124的链接
model binder example和
a better model binder来编写我自己的模型绑定逻辑.结果看起来像这样:
public interface IFilteredModelBinder : IModelBinder { bool IsMatch(Type modelType); } public class SmartModelBinder : DefaultModelBinder { private readonly IFilteredModelBinder[] _filteredModelBinders; public SmartModelBinder(IFilteredModelBinder[] filteredModelBinders) { _filteredModelBinders = filteredModelBinders; } public override object BindModel(ControllerContext controllerContext,ModelBindingContext bindingContext) { foreach (var filteredModelBinder in _filteredModelBinders) { if (filteredModelBinder.IsMatch(bindingContext.ModelType)) { return filteredModelBinder.BindModel(controllerContext,bindingContext); } } return base.BindModel(controllerContext,bindingContext); } } public class NewtonsoftJsonModelBinder : IFilteredModelBinder { public object BindModel(ControllerContext controllerContext,ModelBindingContext bindingContext) { if (!controllerContext.HttpContext.Request.ContentType.StartsWith("application/json",StringComparison.OrdinalIgnoreCase)) { // not JSON request return null; } var request = controllerContext.HttpContext.Request; request.InputStream.Position = 0; var incomingData = new StreamReader(request.InputStream).ReadToEnd(); if (String.IsNullOrEmpty(incomingData)) { // no JSON data return null; } object ret = JsonConvert.DeserializeObject(incomingData,bindingContext.ModelType); return ret; } public bool IsMatch(Type modelType) { var ret = (typeof(JsonModel).IsAssignableFrom(modelType)); return ret; } }
然后,我使用JSON.net属性映射到模型上的不同对象属性(而不是DataContracts).这些模型都继承自空基类JsonModel.