Home  >  Article  >  Backend Development  >  How to solve the problem of JSON loop call in ASP.NET MVC?

How to solve the problem of JSON loop call in ASP.NET MVC?

零下一度
零下一度Original
2017-06-23 15:17:172011browse

1. The Net open source Json serialization tool Newtonsoft.Json provides a solution to the circular reference problem of serialization:

Method 1: Specify the Json serialization configuration as ReferenceLoopHandling.Ignore

Method 2: Specify JsonIgnore to ignore the reference object

Instance 1, solve the Json serialization reference method of MVC:

step1: Add a reference to the Newtonsoft.Json package on the project , Command: Insert-Package Newtonsoft.Json

step2: Add a class to the project, inheriting JsonResult, the code is as follows:

How to solve the problem of JSON loop call in ASP.NET MVC?
/// <summary>/// 继承JsonResut,重写序列化方式/// </summary>public class JsonNetResult : JsonResult
{public JsonSerializerSettings Settings { get; private set; }public JsonNetResult()
    {
        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());
        }
    }
}
How to solve the problem of JSON loop call in ASP.NET MVC?

step3: Add BaseController to the project and rewrite the Json() method, the code is as follows:

How to solve the problem of JSON loop call in ASP.NET MVC?##
public class BaseController : Controller
{public StudentContext _Context = new StudentContext();/// <summary>/// 重写,Json方法,使之返回JsonNetResult类型/// </summary>protected override JsonResult Json(object data, string contentType,
        Encoding contentEncoding, JsonRequestBehavior behavior)
    {return new JsonNetResult
        {
            Data = data,
            ContentType = contentType,
            ContentEncoding = contentEncoding,
            JsonRequestBehavior = behavior
        };
    }
}
How to solve the problem of JSON loop call in ASP.NET MVC?
step4. Just use it as usual

How to solve the problem of JSON loop call in ASP.NET MVC?##
//获取列表public JsonResult GetList()
{
    List<student> list = _Context.students.Where(q => q.sno == "103").ToList();//方法1return Json(list);//方法2//return new JsonNetResult() {//    Data=list//};
}</student>
How to solve the problem of JSON loop call in ASP.NET MVC?
The obtained results indicate that this method specifies to ignore circular references. It is ignored after specifying the cycle series. There is still some circular data in the returned json data

To solve the EF Json serialization circular reference method 2, add the JsonIgnore method annotation on the specified associated object

[JsonIgnore]public virtual ICollection<score> scores { get; set; }</score>
There is no associated table data in the returned results


The article is reproduced from:

The above is the detailed content of How to solve the problem of JSON loop call in ASP.NET MVC?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn