Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.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
C# WebApi中的JSON.NET序列化错误_C#_Json_Serialization_Asp.net Web Api_Json.net - Fatal编程技术网

C# WebApi中的JSON.NET序列化错误

C# WebApi中的JSON.NET序列化错误,c#,json,serialization,asp.net-web-api,json.net,C#,Json,Serialization,Asp.net Web Api,Json.net,我有一个POCO类,它继承了几个类,为它提供INotifyPropertyChanged和DataAnnotations支持。当WebApi返回Court实例时,JSON.NET序列化程序会阻塞ModelPropertyNotationsValidation中的匿名方法委托,出现异常(显示Fiddler的原始响应): {“消息”:“发生了错误”,“异常消息”:“ 'ObjectContent'1'类型未能序列化的响应正文 内容类型'application/json; charset=utf-8'

我有一个POCO类,它继承了几个类,为它提供INotifyPropertyChanged和DataAnnotations支持。当WebApi返回Court实例时,JSON.NET序列化程序会阻塞ModelPropertyNotationsValidation中的匿名方法委托,出现异常(显示Fiddler的原始响应):

{“消息”:“发生了错误”,“异常消息”:“ 'ObjectContent'1'类型未能序列化的响应正文 内容类型'application/json; charset=utf-8',“异常类型”:“System.InvalidOperationException”,“StackTrace”:null,“InnerException”:{“Message”:“An” 发生错误。“,“ExceptionMessage”:“从中获取值时出错。” 上的“CS$9_CachedAnonymousMethodDelegate5” “Sample.Data.Models.Court”,“ExceptionType”:“Newtonsoft.Json.JsonSerializationException”,“StackTrace”: 位于Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(对象 目标)\r\n位于 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues(JsonWriter 编写器,对象值,JsonContainerContract,JsonProperty 成员、JsonProperty属性、JsonContract和memberContract、对象& memberValue)\r\n位于 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializationObject(JsonWriter 编写器,对象值,JsonObjectContract,JsonProperty 成员,JsonContainerContract集合合同,JsonProperty containerProperty)\r\n位于 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializationValue(JsonWriter 编写器,对象值,JsonContract valueContract,JsonProperty成员, JsonContainerContract集装箱合同,JsonProperty containerProperty)\r\n位于 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serializate(JsonWriter jsonWriter,对象值)\r\n位于 Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter,对象值)\r\n位于 System.Net.Http.Formatting.JsonMediaTypeFormatter.c\u DisplayClassd.b\u c()\r\n 在System.Threading.Tasks.TaskHelpers.RunSynchronously(操作, CancellationToken token)”,“InnerException”:{“Message”:“出现错误。” 发生。“,“ExceptionMessage”:“公共语言运行库检测到 无效的 程序。“,“例外类型”:“System.InvalidProgrameException”,“StackTrace”: 在GetCS$9\u CachedAnonymousMethodDelegate5(对象)\r\n在 Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(对象 目标“}}”

法庭级别(为简洁起见修订):

这是问题所在的继承的抽象类(如果我将
公共字符串this[string columnName]
上的getter更改为返回空字符串,它会工作:

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;

namespace Sample.Data.Models.Infrastructure
{
    public abstract class ModelPropertyAnnotationsValidation : ModelBase
    {
        public string this[string columnName]
        {
            get
            {
                var type = GetType();
                var modelProperties = TypeDescriptor.GetProperties(type).Cast<PropertyDescriptor>();

                var enumerable = from modelProperty in modelProperties.Where(modelProp => modelProp.Name == columnName)
                                 from attribute in modelProperty.Attributes.OfType<ValidationAttribute>().Where(attribute => !attribute.IsValid(modelProperty.GetValue(this)))
                                 select attribute.ErrorMessage;
                return enumerable.FirstOrDefault();
            }
            private set { ; } //http://developerstreasure.blogspot.com/2010/05/systemruntimeserializationinvaliddataco.html
        }

        public string Error
        {
            get { return null; }
            private set { ; } //http://developerstreasure.blogspot.com/2010/05/systemruntimeserializationinvaliddataco.html
        }


        public virtual bool IsValid
        {
            get
            {
                var validationContext = new ValidationContext(this, null, null);
                var valid = Validator.TryValidateObject(this, validationContext, null, validateAllProperties: true);
                return valid;
            }
            private set { ; } //http://developerstreasure.blogspot.com/2010/05/systemruntimeserializationinvaliddataco.html
        }
    }
}

我如何才能获得我认为匿名委托通过序列化的内容…是否被忽略?

您使用的Json.NET版本是什么?如果您没有使用最新版本(5.0.8)然后升级到它并再次运行您的应用程序。

4.5.11安装了WebApi,我不想通过NuGet检查更新。现在它是5.0.8并且正常工作,所以这绝对是问题所在。感谢James…给出答案和Json.NET.)
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;

namespace Sample.Data.Models.Infrastructure
{
    public abstract class ModelPropertyAnnotationsValidation : ModelBase
    {
        public string this[string columnName]
        {
            get
            {
                var type = GetType();
                var modelProperties = TypeDescriptor.GetProperties(type).Cast<PropertyDescriptor>();

                var enumerable = from modelProperty in modelProperties.Where(modelProp => modelProp.Name == columnName)
                                 from attribute in modelProperty.Attributes.OfType<ValidationAttribute>().Where(attribute => !attribute.IsValid(modelProperty.GetValue(this)))
                                 select attribute.ErrorMessage;
                return enumerable.FirstOrDefault();
            }
            private set { ; } //http://developerstreasure.blogspot.com/2010/05/systemruntimeserializationinvaliddataco.html
        }

        public string Error
        {
            get { return null; }
            private set { ; } //http://developerstreasure.blogspot.com/2010/05/systemruntimeserializationinvaliddataco.html
        }


        public virtual bool IsValid
        {
            get
            {
                var validationContext = new ValidationContext(this, null, null);
                var valid = Validator.TryValidateObject(this, validationContext, null, validateAllProperties: true);
                return valid;
            }
            private set { ; } //http://developerstreasure.blogspot.com/2010/05/systemruntimeserializationinvaliddataco.html
        }
    }
}
using System;
using System.ComponentModel;
using System.Linq.Expressions;

namespace Sample.Data.Models.Infrastructure
{
    public abstract class ModelBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(Expression<Func<object>> property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(BindingHelper.Name(property)));
            }
        }
    }
}
using System.Web.Http;
using Sample.Data.Models;

namespace Sample.Services.Api.Controllers
{
    public class CourtsController : ApiController
    {
        // GET api/values
        public Court Get()
        {
            return new Court {Abbreviation = "TEST", FullName = "Test Court", Active = true};
        }

    }
}