Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.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# ASP.NET API序列化具有循环依赖项的实体_C#_Asp.net_Serialization - Fatal编程技术网

C# ASP.NET API序列化具有循环依赖项的实体

C# ASP.NET API序列化具有循环依赖项的实体,c#,asp.net,serialization,C#,Asp.net,Serialization,我有Test实体和User实体 测试: 这将返回一个序列化异常,因为它尝试循环序列化用户对象,然后对其测试对象,然后对其用户对象进行序列化,依此类推 我当前的计划是创建一个TestDTO,它包含一个IList用户ID,而不是实际的对象。然后我必须在序列化之前将所有Test对象转换为TestDTO对象,这有点烦人。然后,如果我想要序列化具有循环依赖关系的其他实体,我必须为它们创建更多的数据传输对象 是否有一种方法可以创建或配置序列化程序,以便在此上下文中将User(或任何其他)对象自动序列化为其I

我有
Test
实体和
User
实体

测试:

这将返回一个序列化异常,因为它尝试循环序列化
用户
对象,然后对其
测试
对象,然后对其
用户
对象进行序列化,依此类推

我当前的计划是创建一个
TestDTO
,它包含一个
IList用户ID
,而不是实际的对象。然后我必须在序列化之前将所有
Test
对象转换为
TestDTO
对象,这有点烦人。然后,如果我想要序列化具有循环依赖关系的其他实体,我必须为它们创建更多的数据传输对象

是否有一种方法可以创建或配置序列化程序,以便在此上下文中将
User
(或任何其他)对象自动序列化为其
Id
属性

异常编辑: 无论是使用
XML
还是
JSON

<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>
The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'.
</ExceptionMessage>
<ExceptionType>System.InvalidOperationException</ExceptionType>
<StackTrace/>
<InnerException>
<Message>An error has occurred.</Message>
<ExceptionMessage>
Type 'System.Data.Entity.DynamicProxies.Test_6585C5F85A384907958ABA03B661933EF718BDEDE1513392E060DE06E80DB552' with data contract name 'Test_6585C5F85A384907958ABA03B661933EF718BDEDE1513392E060DE06E80DB552:http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.
</ExceptionMessage>
<ExceptionType>
System.Runtime.Serialization.SerializationException
</ExceptionType>
<StackTrace>...</StackTrace>
</InnerException>
</Error>

发生了一个错误。
'ObjectContent'1'类型未能序列化内容类型'application/xml'的响应正文;字符集=utf-8'。
System.InvalidOperationException异常
发生了一个错误。
键入“System.Data.Entity.DynamicProxies.Test”和数据合同名为“Test”的“System.Data.Entity.DynamicProxies.Test”6585C5F85A384907958ABA03B661933EF718BDEDE1513392E060DE06E80DB552:http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies”他说。考虑使用DATACONTRORTCORDEVER或将未知类型的任何类型添加到已知类型的列表中,例如,使用NoNyType属性或将它们添加到传递给DATACONTROTTRORIGLASER的已知类型列表中。
System.Runtime.Serialization.SerializationException
...
在您的web api配置(App\u Start/WebApiConfig.cs)中,添加以下内容:

    public static class WebApiConfig
    {
      public static void Register(HttpConfiguration config)
      {
        // Prevent "Self referencing loop detected" error occurring for recursive objects
        var serializerSettings = new JsonSerializerSettings()
        {
            ReferenceLoopHandling = ReferenceLoopHandling.Ignore
        };
        config.Formatters.JsonFormatter.SerializerSettings = serializerSettings;
      }
    }
这会告诉JSON.NET忽略在web api配置(App_Start/WebApiConfig.cs)中引用回父对象的嵌套对象,添加以下内容:

    public static class WebApiConfig
    {
      public static void Register(HttpConfiguration config)
      {
        // Prevent "Self referencing loop detected" error occurring for recursive objects
        var serializerSettings = new JsonSerializerSettings()
        {
            ReferenceLoopHandling = ReferenceLoopHandling.Ignore
        };
        config.Formatters.JsonFormatter.SerializerSettings = serializerSettings;
      }
    }
这会告诉JSON.NET忽略在web api配置(App_Start/WebApiConfig.cs)中引用回父对象的嵌套对象,添加以下内容:

    public static class WebApiConfig
    {
      public static void Register(HttpConfiguration config)
      {
        // Prevent "Self referencing loop detected" error occurring for recursive objects
        var serializerSettings = new JsonSerializerSettings()
        {
            ReferenceLoopHandling = ReferenceLoopHandling.Ignore
        };
        config.Formatters.JsonFormatter.SerializerSettings = serializerSettings;
      }
    }
这会告诉JSON.NET忽略在web api配置(App_Start/WebApiConfig.cs)中引用回父对象的嵌套对象,添加以下内容:

    public static class WebApiConfig
    {
      public static void Register(HttpConfiguration config)
      {
        // Prevent "Self referencing loop detected" error occurring for recursive objects
        var serializerSettings = new JsonSerializerSettings()
        {
            ReferenceLoopHandling = ReferenceLoopHandling.Ignore
        };
        config.Formatters.JsonFormatter.SerializerSettings = serializerSettings;
      }
    }

这告诉JSON.NET忽略引用父对象的嵌套对象构建JSON结果的一种非常简单的方法是使用NewtonSoft JSON序列化程序中的JObject(这通常是ASP.NET Web Api中的默认值)。你要找的东西看起来像

public JObject GetTests()
{

    NewtonSoft.Json.Linq.JObject jsonResult = NewtonSoft.Json.Linq.JObject.FromObject(new 
    {
        Tests = from test in db.tests
                select new 
                {
                    Id = test.Id,
                    Title = test.Title,
                    Users = from user in test.Users
                            select new
                            {
                                Id = user.Id
                            }
                }
    });

    return jsonResult;
}
这使您能够更好地控制json结果,从而可以选择要序列化的字段/属性,并避免创建大量数据传输对象。希望这有帮助


Cheers

构造json结果的一种非常简单的方法是使用NewtonSoft json序列化程序中的JObject(这通常是ASP.NET Web Api中的默认值)。你要找的东西看起来像

public JObject GetTests()
{

    NewtonSoft.Json.Linq.JObject jsonResult = NewtonSoft.Json.Linq.JObject.FromObject(new 
    {
        Tests = from test in db.tests
                select new 
                {
                    Id = test.Id,
                    Title = test.Title,
                    Users = from user in test.Users
                            select new
                            {
                                Id = user.Id
                            }
                }
    });

    return jsonResult;
}
这使您能够更好地控制json结果,从而可以选择要序列化的字段/属性,并避免创建大量数据传输对象。希望这有帮助


Cheers

构造json结果的一种非常简单的方法是使用NewtonSoft json序列化程序中的JObject(这通常是ASP.NET Web Api中的默认值)。你要找的东西看起来像

public JObject GetTests()
{

    NewtonSoft.Json.Linq.JObject jsonResult = NewtonSoft.Json.Linq.JObject.FromObject(new 
    {
        Tests = from test in db.tests
                select new 
                {
                    Id = test.Id,
                    Title = test.Title,
                    Users = from user in test.Users
                            select new
                            {
                                Id = user.Id
                            }
                }
    });

    return jsonResult;
}
这使您能够更好地控制json结果,从而可以选择要序列化的字段/属性,并避免创建大量数据传输对象。希望这有帮助


Cheers

构造json结果的一种非常简单的方法是使用NewtonSoft json序列化程序中的JObject(这通常是ASP.NET Web Api中的默认值)。你要找的东西看起来像

public JObject GetTests()
{

    NewtonSoft.Json.Linq.JObject jsonResult = NewtonSoft.Json.Linq.JObject.FromObject(new 
    {
        Tests = from test in db.tests
                select new 
                {
                    Id = test.Id,
                    Title = test.Title,
                    Users = from user in test.Users
                            select new
                            {
                                Id = user.Id
                            }
                }
    });

    return jsonResult;
}
这使您能够更好地控制json结果,从而可以选择要序列化的字段/属性,并避免创建大量数据传输对象。希望这有帮助


干杯

我想说这里的问题是设计,而不是序列化程序。考虑< <代码>测试> /代码> s确实需要一个“代码>用户< /代码> s的列表。如果是这样的话,我会存储一个ID列表,而不是实际的对象。它存储
用户
对象而不是它们的
Id
在我的应用程序的其他地方非常有用。我想说这里的问题是设计,而不是序列化程序。考虑< <代码>测试> /代码> s确实需要一个“代码>用户< /代码> s的列表。如果是这样的话,我会存储一个ID列表,而不是实际的对象。它存储
用户
对象而不是它们的
Id
在我的应用程序的其他地方非常有用。我想说这里的问题是设计,而不是序列化程序。考虑< <代码>测试> /代码> s确实需要一个“代码>用户< /代码> s的列表。如果是这样的话,我会存储一个ID列表,而不是实际的对象。它存储
用户
对象而不是它们的
Id
在我的应用程序的其他地方非常有用。我想说这里的问题是设计,而不是序列化程序。考虑< <代码>测试> /代码> s确实需要一个“代码>用户< /代码> s的列表。如果是这样的话,我会存储一个ID列表,而不是实际的对象。它存储的是
用户
对象,而不是它们的
Id
s,这一事实在我的应用程序的其他地方非常有用。我编辑了我的问题,把它包括进去。我只是这么做了,我仍然得到同样的例外。我编辑了我的问题,把它包括进去。我只是这么做了,我仍然得到同样的例外。我编辑了我的问题,把它包括进去。我只是这么做了,我仍然得到同样的例外。我编辑了我的问题,将其包括在内。我不会这样做