如何解决如何配置JsonResult序列化器
从控制器返回一些数据到视图时,我遇到一个问题,一个非常基本的Dictionary
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Login(Loginviewmodel model)
{
if(ModelState.IsValid)
{
try
{
var userExists = db.UserAuth.Where(ua => ua.Email == model.Email).Count() > 0;
if (userExists)
{
var uAuth = db.UserAuth.Where(ua => ua.Email == model.Email).ToList();
if (uAuth[0].Senha == model.Senha)
{
if (Request.Cookies.Get("CookieLogged") == null)
{
var CookieLogged = new HttpCookie("CookieLogged");
CookieLogged.Value = "true";
CookieLogged.Expires = DateTime.Now.AddHours(4);
CookieLogged.SameSite = SameSiteMode.Strict;
Response.Cookies.Add(CookieLogged);
}
else
{
var Logged = Request.Cookies.Get("CookieLogged");
Logged.Value = "true";
Logged.Expires = DateTime.Now.AddHours(4);
Response.Cookies.Set(Logged);
}
var JsonRedirect = new Dictionary<string,string>();
JsonRedirect.Add("redirect",Url.Action("Index","Home"));
return Json(JsonRedirect);
}
else
{
var JsonWarning = new Dictionary<string,string>();
JsonWarning.Add("warning","Senha não confere");
string jsonString = JsonConvert.SerializeObject(JsonWarning);
return Content(jsonString,"application/json");
}
}
else
{
var JsonWarning = new Dictionary<string,string>();
JsonWarning.Add("warning","Email não confere ou usuário não existe");
return Json(JsonWarning,JsonRequestBehavior.AllowGet);
}
}
catch (Exception ex)
{
var JsonError = new Dictionary<string,Exception>();
JsonError.Add("errorEx",ex);
return Json(JsonError,JsonRequestBehavior.AllowGet);
}
}
else
{
IDictionary<string,string> JsonWarning = new Dictionary<string,string>();
JsonWarning.Add("warning","Dados inválidos");
return Content(JsonConvert.SerializeObject(JsonWarning),"application/json");
}
}
它在本地运行正常,但是一旦我将其发布到托管站点,它就会中断。请求返回500,错误消息显示system.invalidOperationException: A circular reference was detected while serializing an object of type 'System.Reflection.RuntimeModule'.
,堆栈跟踪提示我问题可能出在字典的序列化方式上,但这只是一个猜测。错误的堆栈跟踪:
System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o,StringBuilder sb,Int32深度,Hashtable objectsInUse,SerializationFormat serializationFormat,MemberInfo currentMember)+1446 System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(对象o,StringBuilder sb,Int32深度,Hashtable objectsInUse,SerializationFormat serializationFormat,MemberInfo currentMember)+195 System.Web.Script.Serialization.JavaScriptSerializer.SerializeCustomObject(Object o,StringBuilder sb,Int32深度,Hashtable objectsInUse,SerializationFormat serializationFormat)+480 System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(对象o,StringBuilder sb,Int32深度,哈希表objectsInUse,SerializationFormat serializationFormat,MemberInfo currentMember)+1324 System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(对象o,StringBuilder sb,Int32深度,Hashtable objectsInUse,SerializationFormat serializationFormat,MemberInfo currentMember)+195 System.Web.Script.Serialization.JavaScriptSerializer.SerializeEnumerable(IEnumerable可枚举,StringBuilder sb,Int32深度,Hashtable objectsInUse,SerializationFormat serializationFormat)+127 System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(对象o,StringBuilder sb,Int32深度,哈希表objectsInUse,SerializationFormat serializationFormat,MemberInfo currentMember)+1279 System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(对象o,StringBuilder sb,Int32深度,Hashtable objectsInUse,SerializationFormat serializationFormat,MemberInfo currentMember)+195 System.Web.Script.Serialization.JavaScriptSerializer.SerializeCustomObject(Object o,StringBuilder sb,Int32深度,Hashtable objectsInUse,SerializationFormat serializationFormat)+480 System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(对象o,StringBuilder sb,Int32深度,哈希表objectsInUse,SerializationFormat serializationFormat,MemberInfo currentMember)+1324 System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(对象o,StringBuilder sb,Int32深度,Hashtable objectsInUse,SerializationFormat serializationFormat,MemberInfo currentMember)+195 System.Web.Script.Serialization.JavaScriptSerializer.SerializeCustomObject(Object o,StringBuilder sb,Int32深度,Hashtable objectsInUse,SerializationFormat serializationFormat)+480 System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(对象o,StringBuilder sb,Int32深度,哈希表objectsInUse,SerializationFormat serializationFormat,MemberInfo currentMember)+1324 System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(对象o,StringBuilder sb,Int32深度,Hashtable objectsInUse,SerializationFormat serializationFormat,MemberInfo currentMember)+195 System.Web.Script.Serialization.JavaScriptSerializer.SerializeCustomObject(Object o,StringBuilder sb,Int32深度,Hashtable objectsInUse,SerializationFormat serializationFormat)+480 System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(对象o,StringBuilder sb,Int32深度,哈希表objectsInUse,SerializationFormat serializationFormat,MemberInfo currentMember)+1324 System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(对象o,StringBuilder sb,Int32深度,Hashtable objectsInUse,SerializationFormat serializationFormat,MemberInfo currentMember)+195 System.Web.Script.Serialization.JavaScriptSerializer.SerializeCustomObject(Object o,StringBuilder sb,Int32深度,Hashtable objectsInUse,SerializationFormat serializationFormat)+480 System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(对象o,StringBuilder sb,Int32深度,哈希表objectsInUse,SerializationFormat serializationFormat,MemberInfo currentMember)+1324 System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(对象o,StringBuilder sb,Int32深度,Hashtable objectsInUse,SerializationFormat serializationFormat,MemberInfo currentMember)+195 System.Web.Script.Serialization.JavaScriptSerializer.SerializeCustomObject(Object o,StringBuilder sb,Int32深度,Hashtable objectsInUse,SerializationFormat serializationFormat)+480 System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(对象o,StringBuilder sb,Int32深度,哈希表objectsInUse,SerializationFormat serializationFormat,MemberInfo currentMember)+1324 System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(对象o,StringBuilder sb,Int32深度,Hashtable objectsInUse,SerializationFormat serializationFormat,MemberInfo currentMember)+195 System.Web.Script.Serialization.JavaScriptSerializer.SerializeDictionary(IDictionary o,StringBuilder sb,Int32深度,Hashtable objectsInUse,SerializationFormat serializationFormat)+505 System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(对象o,StringBuilder sb,Int32深度,哈希表objectsInUse,SerializationFormat serializationFormat,MemberInfo currentMember)+1218 System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(对象o,StringBuilder sb,Int32深度,Hashtable objectsInUse,SerializationFormat serializationFormat,MemberInfo currentMember)+195 System.Web.Script.Serialization.JavaScriptSerializer.Serialize(对象obj,StringBuilder输出,SerializationFormat serializationFormat)+29 System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj,SerializationFormat serializationFormat)+64 System.Web.Mvc.JsonResult.ExecuteResult(ControllerContext上下文)+347 System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext,ActionResult actionResult)+13 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList
1 filters,Int32 filterIndex,ResultExecutingContext preContext,ControllerContext controllerContext,ActionResult actionResult) +56 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList
1个过滤器,Int32 filterIndex,ResultExecutingContext preContext,ControllerContext controllerContext,ActionResult actionResult)+420 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext,IList1 filters,ActionResult actionResult) +52 System.Web.Mvc.Async.<>c__displayClass3_6.<BeginInvokeAction>b__4() +198 System.Web.Mvc.Async.<>c__displayClass3_1.<BeginInvokeAction>b__1(IAsyncResult asyncResult) +100 System.Web.Mvc.Async.WrappedAsyncResult
1.CallEndDelegate(IAsyncResult asyncResult)+10 System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +49 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27 System.Web.Mvc.<>c.<BeginExecuteCore>b__152_1(IAsyncResult asyncResult,ExecuteCoreState innerState) +11 System.Web.Mvc.Async.WrappedAsyncVoid
1.CallEndDelegate(IAsyncResult asyncResult)+29 System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +49 System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +45 System.Web.Mvc.<>c.<BeginExecute>b__151_2(IAsyncResult asyncResult,Controller controller) +13 System.Web.Mvc.Async.WrappedAsyncVoid
1.CallEndDelegate(IAsyncResult asyncResult)+22 System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +49 System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +26 System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10 System.Web.Mvc.<>c.<BeginProcessRequest>b__20_1(IAsyncResult asyncResult,ProcessRequestState innerState) +28 System.Web.Mvc.Async.WrappedAsyncVoid
1.CallEndDelegate(IAsyncResult asyncResult)+29 System.Web.Mvc.Async.WrappedAsyncResultBase`1.End()+49 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)+28 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult结果)+9 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()+577 System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep步骤)+132 System.Web.HttpApplication.ExecuteStep(IExecutionStep步骤,布尔值并已完成同步)+163
那么,有没有办法防止序列化程序进入此循环?我还尝试过进行序列化并使用return Content()
发送到视图,但是结果是相同的。我对该主题的研究表明EF类和DataTables存在此问题,所以我真的不明白为什么我的简单字典没有通过。预先感谢。
解决方法
我不确定您为什么要使用字典,但是您可以尝试以下方法:
return Json(new { redirect = Url.Action("Index","Home") });
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。