本文介绍了ModelBinding不反序列化json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在vs 2012中,我正在使用带有Entity Framework 6的MVC 5,Web API 2.

I'm using MVC 5, Web API 2 with Entity Framework 6 in vs 2012.

我能够在Web api控制器中成功调用正确的操作,但是传入的json不会反序列化,或者至少不会正确地反序列化. modelBinding识别出请求中的json与Post操作的参数Type相匹配,但是每个属性均为null或默认值.

I'm able to successfully call the right action in my web api controller but the incoming json is never deserialized, or at least not properly. The modelBinding recognizes that the json in the request matches the argument Type for the Post action but every property is null or default value.

为什么不对json进行反序列化?我在这里看不到任何问题.

Why is the json not being deserialized? I can't see any issues here.

这不是有效的网址,只是我本地上的某个设置

身体:

{
    "scab" : {
        "url" : "http://money.cnn.com/2013/12/18/news/companies/target-credit-card/index.html?iid=Lead",
        "title" : "Target: 40 million credit cards compromised",
        "description" : "The Secret Service is investigating a reported credit card data breach at discount retailer Target.",
        "image" : {
            "height" : 367,
            "width" : 620,
            "url" : "http://i2.cdn.turner.com/money/dam/assets/131128213407-black-friday-target-pa-620xa.jpg"
        },
        "category" : null
    }
}

请求标头:

Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:no-cache
Connection:keep-alive
Content-Length:422
Content-Type:application/json
Cookie:.ASPXAUTH=DA97AB5D720AC3100F9698DEAED044403CC5BA266EA8F3361E72C183DA2FAD5EB76D0D9DC2AB6DE30507FF788B7D6607473F87B6EE0D28C043DA50508407ABA1FC7ADA9B8A61F2A7C95024869064EE5D9C6863C670FEF221F9447A3D1F2E7CA67849A33B45B0776FFF0D76CD5C290500
Host:www.scabs.com
Origin:chrome-extension://fdmmgilgnpjigdojojpjoooidkmcomcm
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36

控制器:

public class ScabsController : ApiController {

    private ScabStore _store;

    private ScabStore store {
        get { return _store = _store ?? new ScabStore(); }
    }

    // POST api/scabs
    public void Post(Scab scab) {
        store.scabRepository.save(scab);
        store.commit();
    }
}

结ca类型

public class Scab {
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int id { get; set; }

    [DataType(DataType.Url)]
    public string url { get; set; }
    public string title { get; set; }
    public string description { get; set; }
    public string site { get; set; }
    public virtual Category category { get; set; }
    public virtual Image image { get; set; } // Complex Type
    public virtual User postedBy { get; set; }
    public virtual ICollection<Score> scores { get; set; }
    public virtual ICollection<Comment> comments { get; set; }

    public Scab() {
        scores = new List<Score>();
        comments = new List<Comment>();
    }
}

我还应该注意,Image属性是ComplexType

I should also note that the Image property is a ComplexType

推荐答案

您的JSON数据与您的C#模型不匹配.

Your JSON data does not match your C# model.

因此,您需要删除不必要的"scab"属性,并且数据应如下所示:

So you need to drop the unnecessary "scab" property and your data should look like this:

{
    "url" : "http://money.cnn.com/2013/12/18/news/companies/target-credit-card/index.html?iid=Lead",
    "title" : "Target: 40 million credit cards compromised",
    "description" : "The Secret Service is investigating a reported credit card data breach at discount retailer Target.",
    "image" : {
        "height" : 367,
        "width" : 620,
        "url" : "http://i2.cdn.turner.com/money/dam/assets/131128213407-black-friday-target-pa-620xa.jpg"
    },
    "category" : null
}

或者,如果您不能更改客户端,请将C#操作代码修改为:

Or if you cannot change the client side modify your C# action code to:

public void Post(SaveScabModel scabModel) {
    store.scabRepository.save(scabModel);
    store.commit();
}

SaveScabModel只是包装了Scab模型,以便在c#侧也具有额外"结疤:

Where SaveScabModel just wraps the Scab model to have the "extra" scab also on the c# side:

public class SaveScabModel {
    public Scab Scab { get; set; }
}

这篇关于ModelBinding不反序列化json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 10:03