从获取请求返回AspNetUser表数据

时间:2018-06-30 18:32:13

标签: asp.net asp.net-web-api asp.net-web-api2 asp.net-identity owin

我正在使用Asp.Net Web Api身份-个人用户帐户。我从Web API返回AspNetUser表数据。 注意:此表是由我的asp.net本身而非我自己构成的。但是,我更改了它的连接字符串,以将其存储在Sql中预先存在的数据库中,现在,返回表数据会导致Internal Server Error

public AspNetUser Get(string id)
    {
            using (UserDBEntities entities = new UserDBEntities())
            {
                return entities.AspNetUsers.FirstOrDefault(u => u.UserName == id);
            }
    }

根据个人用户帐户的建议,AspNetUser表的类是默认类。

如果我像下面那样执行操作,错误仍然会在正常运行时持续存在,但在调试时却不会持续存在,也就是说,当我逐步调试代码时,它可以工作并返回正确的数据,但是当我删除断点并执行,或者在调试时按继续键,而不是逐步返回语句,这会出错。

AspNetUser data = entities.AspNetUsers.FirstOrDefault(u => u.UserName == id);
return data;

与这种情况不同,在不存储数据的情况下返回数据在所有情况下(无论是否进行调试)都会出错。

但是,我不明白,为什么? 这怎么会影响呢?

网络错误响应正文:=>

{"Message":"An error has occurred.","ExceptionMessage":"The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'.","ExceptionType":"System.InvalidOperationException","StackTrace":null,"InnerException":{"Message":"An error has occurred.","ExceptionMessage":"Error getting value from 'AspNetUserClaims' on 'System.Data.Entity.DynamicProxies.AspNetUser_4C1FDBB9E07016D6C527FBA4F5785F2C7FE37C77A86A5D1946B99CFFDE56DB7F'.","ExceptionType":"Newtonsoft.Json.JsonSerializationException","StackTrace":"   at Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(Object target)\r\n   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues(JsonWriter writer, Object value, JsonContainerContract contract, JsonProperty member, JsonProperty property, JsonContract& memberContract, Object& memberValue)\r\n   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)\r\n   at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)\r\n   at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, Encoding effectiveEncoding)\r\n   at System.Net.Http.Formatting.JsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, Encoding effectiveEncoding)\r\n   at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, HttpContent content)\r\n   at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.WebHost.HttpControllerHandler.<WriteBufferedResponseContentAsync>d__22.MoveNext()","InnerException":{"Message":"An error has occurred.","ExceptionMessage":"The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.","ExceptionType":"System.ObjectDisposedException","StackTrace":"   at System.Data.Entity.Core.Objects.ObjectContext.get_Connection()\r\n   at System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)\r\n   at System.Data.Entity.Core.Objects.ObjectQuery`1.Execute(MergeOption mergeOption)\r\n   at System.Data.Entity.Core.Objects.DataClasses.EntityCollection`1.Load(List`1 collection, MergeOption mergeOption)\r\n   at System.Data.Entity.Core.Objects.DataClasses.EntityCollection`1.Load(MergeOption mergeOption)\r\n   at System.Data.Entity.Core.Objects.DataClasses.RelatedEnd.DeferredLoad()\r\n   at System.Data.Entity.Core.Objects.Internal.LazyLoadBehavior.LoadProperty[TItem](TItem propertyValue, String relationshipName, String targetRoleName, Boolean mustBeNull, Object wrapperObject)\r\n   at System.Data.Entity.Core.Objects.Internal.LazyLoadBehavior.<>c__DisplayClass7`2.<GetInterceptorDelegate>b__1(TProxy proxy, TItem item)\r\n   at System.Data.Entity.DynamicProxies.AspNetUser_4C1FDBB9E07016D6C527FBA4F5785F2C7FE37C77A86A5D1946B99CFFDE56DB7F.get_AspNetUserClaims()\r\n   at GetAspNetUserClaims(Object )\r\n   at Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(Object target)"}}}

我想知道这种异常的原因,而不是任何解决方案 ,因为可以通过返回定义的类(例如=>

)来解决
            User user = new User();
            AspNetUser data = entities.AspNetUsers.FirstOrDefault(u => u.UserName == id);
            user.reg_username = data.UserName;
            user.reg_email = data.Email;
            return user;

0 个答案:

没有答案