加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – DataContract模型绑定到ASP.NET MVC操作方法参数中的JSON

发布时间:2020-12-15 07:45:04 所属栏目:百科 来源:网络整理
导读:MVC3开箱即用JsonValueProviderFactory(),它非常方便将传入的 JSON绑定到模型.不幸的是,我无法弄清楚如何设置名称与传入JSON不同的模型合同.例如: [DataContract(Name = "session")]public class FacebookSession{ [DataMember(Name = "access_token")] pub
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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读