Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 类上的JSONIgnore数据注释_Asp.net Mvc_Json.net_Data Annotations - Fatal编程技术网

Asp.net mvc 类上的JSONIgnore数据注释

Asp.net mvc 类上的JSONIgnore数据注释,asp.net-mvc,json.net,data-annotations,Asp.net Mvc,Json.net,Data Annotations,我有一个公共属性,它是一个包含许多属性的对象。使用ASP.net MVC,在序列化JSON数据时,只要在使用对象的任何位置添加[JsonIgnore]属性,它就不会显示内容 有没有一种方法可以将[JsonIgnore]属性添加到类中,这样它就不会被序列化 //[JsonIgnore] ?? public class DataObj { public string ConnectionName { get; set; } public string Query { get; se

我有一个公共属性,它是一个包含许多属性的对象。使用ASP.net MVC,在序列化JSON数据时,只要在使用对象的任何位置添加
[JsonIgnore]
属性,它就不会显示内容

有没有一种方法可以将
[JsonIgnore]
属性添加到类中,这样它就不会被序列化

//[JsonIgnore]  ??
public class DataObj
{
    public string ConnectionName { get; set; }
    public string Query { get; set; }
    ...
}

public class Customer
{
    public string First { get; set; }
    public string Last { get; set; }
    [JsonIgnore]
    public DataObj Foo { get; set; }
}

public class ShipAddress
{
    public string Street { get; set; }
    public string City { get; set; }
    public string Country { get; set; }
    [JsonIgnore]
    public DataObj Foo { get; set; }
}
我的解决方案收到提供的代码后。
还有,这里有一个例子可以解释更多

public class DataObjFilterContractResolver : DefaultContractResolver
{
    public static readonly DataObjFilterContractResolver Instance = new DataObjFilterContractResolver();

    protected override JsonProperty CreateProperty(MemberInfo member,MemberSerialization memberSerialization)
    {
        var property = base.CreateProperty(member, memberSerialization);
        if (property.DeclaringType.Name.StartsWith("DataObj") || property.PropertyName == "DataObj")
        {
            property.ShouldSerialize = instance => false;
        }
        return property;
    }
}


public class UtcJsonResult : JsonResult
{
    public UtcJsonResult(object data)
    {
        Data = data;
        JsonRequestBehavior = JsonRequestBehavior.AllowGet;
    }

    private const string DateFormat = @"yyyy-MM-dd HH:mm:ssZ";

    public override void ExecuteResult(ControllerContext context)
    {
        if (context == null) throw new ArgumentNullException("context");
        if (Data == null) return;

        var response = context.HttpContext.Response;
        response.ContentType = !string.IsNullOrEmpty(ContentType) ? ContentType : "application/json";
        if (ContentEncoding != null) response.ContentEncoding = ContentEncoding;

        var isoConvert = new IsoDateTimeConverter {DateTimeFormat = DateFormat};
        JsonConvert.DefaultSettings = 
            () => new JsonSerializerSettings 
                { ContractResolver = new DataObjFilterContractResolver()};  //<--- Used here
        var json = JsonConvert.SerializeObject(Data, isoConvert);
        response.Write(json);
    }
}
公共类DataObjFilterContractResolver:DefaultContractResolver
{
公共静态只读DataObjFilterContractResolver实例=新DataObjFilterContractResolver();
受保护的重写JsonProperty CreateProperty(MemberInfo成员、MemberSerialization MemberSerialization)
{
var property=base.CreateProperty(成员,成员序列化);
if(property.DeclaringType.Name.StartsWith(“DataObj”)| | property.PropertyName==“DataObj”)
{
property.ShouldSerialize=instance=>false;
}
归还财产;
}
}
公共类UtcJsonResult:JsonResult
{
公共UtcJsonResult(对象数据)
{
数据=数据;
JsonRequestBehavior=JsonRequestBehavior.AllowGet;
}
private const string DateFormat=@“yyyy-MM-dd HH:MM:ssZ”;
公共覆盖无效ExecuteSult(ControllerContext上下文)
{
如果(context==null)抛出新的ArgumentNullException(“context”);
if(Data==null)返回;
var response=context.HttpContext.response;
response.ContentType=!string.IsNullOrEmpty(ContentType)?ContentType:“应用程序/json”;
if(ContentEncoding!=null)response.ContentEncoding=ContentEncoding;
var isoConvert=new IsoDateTimeConverter{DateTimeFormat=DateFormat};
JsonConvert.DefaultSettings=
()=>新的JsonSerializerSettings

{ContractResolver=new DataObjFilterContractResolver()};//您可以在项目中添加合同解析器

public class ShouldSerializeContractResolver : DefaultContractResolver
{
    public new static readonly ShouldSerializeContractResolver Instance =
    new ShouldSerializeContractResolver();

    protected override JsonProperty CreateProperty(MemberInfo member,
    MemberSerialization memberSerialization)
    {
        JsonProperty property = base.CreateProperty(member, memberSerialization);

        if (property.DeclaringType == typeof(DataObj))
        {
            property.ShouldSerialize =
                instance =>
                {
                    return false;
                };
        }

        return property;
    }
}

我花了一点时间才弄明白如何在我的设计中实现它,但效果很好。我真的很高兴能帮上忙。:)