EF在转换成JsonResult时遇到无限循环的解决办法

本博客为做者平常工做遇到的一些问题集合,但愿能给热爱编程的初学者一些知识。编程

基于在应用EF实体类,若是存在导航属性,则return Json(List<Entity> list,JsonRequestBehavior.AllowGet)时会遇到无限循环致使报错的问题,因此但愿有办法解决此问题。json

解决思路1:app

1.新增多一个EF模板,把其导航属性去除。ide

在.tt模板中,把类名加Model,与原来的类名区分开来。oop

<#=codeStringGenerator.EntityClassOpening(entity)+"Model"#>
T4模板代码
<#
        foreach (var navigationProperty in navigationProperties)
        {
#>
    <#=codeStringGenerator.NavigationProperty(navigationProperty)#>
<#
        }
#>
去掉这些代码以去除导航属性

 

2.利用一个方法把List<Entity>批量转换成List<EntityModel>。性能

public class Mapper<T,K> 
    {
        /// <summary>
        /// 把List<T>转换成List<K>
        /// </summary>
        /// <param name="listT"></param>
        /// <param name="model">默认new Model()</param>
        /// <returns></returns>
        public static List<K> MapObjectList(List<T> listT, K model)
        {
            BinaryFormatter bf = new BinaryFormatter();
            MemoryStream ms = new MemoryStream();
            bf.Serialize(ms, model); //复制到流中

            List<K> listK = new List<K>();
            foreach (T t in listT)
            {
                ms.Position = 0;
                K k = (K)(bf.Deserialize(ms));

                ObjectMapperManager.DefaultInstance.GetMapper<T, K>().Map(t, k);
                listK.Add(k);
            }
            return listK;
        }
View Code

 

3.可用return Json(List<EntityModel> list,JsonRequestBehavior.AllowGet)学习

这样把其导航属性去掉,前台就不会出现无线循环的报错了。this

 EmitMapper.ObjectMapperManager 能够在nuget里面找到,挺好用的一个类,是用来把EntityA转换成EntityB,把两个实体相同属性的名称spa

对应赋值。.net

  可是,这样作效率彷佛不高,并且还比较麻烦。因而再找找有没有更便捷的方法,毕竟每次都这样又装箱又拆箱,性能损耗也是很多。因而有了解决思路2

 

解决思路2:

 直接利用方法去除类中的导航属性!!

写一个MyJsonResult类

public class MyJsonResult : JsonResult 
    {
        public JsonSerializerSettings Settings { get; private set; } 

        public MyJsonResult()
        { 
            Settings = new JsonSerializerSettings 
            { 
         //这句是解决问题的关键,也就是json.net官方给出的解决配置选项.                 
          ReferenceLoopHandling = ReferenceLoopHandling.Ignore
            }; 
        }

        public override void ExecuteResult(ControllerContext context) 
        { 
            if (context == null)            
                throw new ArgumentNullException("context"); 
            if (this.JsonRequestBehavior == JsonRequestBehavior.DenyGet && string.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))            
                throw new InvalidOperationException("JSON GET is not allowed"); 
            HttpResponseBase response = context.HttpContext.Response; 
            response.ContentType = string.IsNullOrEmpty(this.ContentType) ? "application/json" : this.ContentType; 
            if (this.ContentEncoding != null)            
                response.ContentEncoding = this.ContentEncoding; 
            if (this.Data == null)            
                return; 
            var scriptSerializer = JsonSerializer.Create(this.Settings); 
            using (var sw = new StringWriter()) 
            { 
                scriptSerializer.Serialize(sw, this.Data); 
                response.Write(sw.ToString()); 
            } 
        } 
    }

  而后,在BaseController里重写Json方法
public class BaseController : Controller
    {
        protected override JsonResult Json(object data, string contentType, Encoding contentEncoding, JsonRequestBehavior behavior) 
        { 
            return new MyJsonResult 
            { 
                Data = data, 
                ContentType = contentType, 
                ContentEncoding = contentEncoding, 
                JsonRequestBehavior = behavior }; 
        }

    }

  最后只需在Controller里继承BaseController就好了。很是方便!并且没有转换效率的问题。解决思路2是推荐的办法。固然也在解决思路1中学习到了很多的知识。
若是哪位大神有更好的解决办法,请赐教。
相关文章
相关标签/搜索