Asp.net 当我在ajax调用中返回太多的json行时,为什么会出现Http错误500?
这是我的密码:Asp.net 当我在ajax调用中返回太多的json行时,为什么会出现Http错误500?,asp.net,asp.net-mvc,asp.net-mvc-5,asp.net-mvc-5.2,asp.net-mvc-5.1,Asp.net,Asp.net Mvc,Asp.net Mvc 5,Asp.net Mvc 5.2,Asp.net Mvc 5.1,这是我的密码: var context = _contextFactory.Create(GetClient()); var r = from p in context.MyTable.ToList() select p; int tot; var firstPageData = PagedResult(r, 0, 200000, rows => new { rows.I
var context = _contextFactory.Create(GetClient());
var r = from p in context.MyTable.ToList()
select p;
int tot;
var firstPageData = PagedResult(r, 0, 200000, rows => new { rows.Id }, true, out tot);
return Json(new { result = "ok", rows = firstPageData }, JsonRequestBehavior.AllowGet);
PagedResult()的第一个和第二个参数(0和200000)是要返回的页码和行数。这将返回http错误500
但是当我将第二个参数更改为PagedResult()以仅返回20行时,它工作正常。像这样:
var context = _contextFactory.Create(GetClient());
var r = from p in context.MyTable.ToList()
select p;
int tot;
var firstPageData = PagedResult(r, 0, 20, rows => new { rows.Id }, true, out tot);
return Json(new { result = "ok", rows = firstPageData }, JsonRequestBehavior.AllowGet);
是不是因为我返回了太多http无法处理的行,所以出现了这个错误?或者是否需要进行配置,以便返回太多的行
谢谢,为ASP.NETMVC使用自定义的JsonResult类,以避免超出MaxJsonLength的异常
public class LargeJsonResult : JsonResult
{
const string JsonRequest_GetNotAllowed = "This request has been blocked because sensitive information could be disclosed to third party web sites when this is used in a GET request. To allow GET requests, set JsonRequestBehavior to AllowGet.";
public LargeJsonResult()
{
MaxJsonLength = 1024000;
RecursionLimit = 100;
}
public int MaxJsonLength { get; set; }
public int RecursionLimit { get; set; }
public override void ExecuteResult( ControllerContext context )
{
if( context == null )
{
throw new ArgumentNullException( "context" );
}
if( JsonRequestBehavior == JsonRequestBehavior.DenyGet &&
String.Equals( context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase ) )
{
throw new InvalidOperationException( JsonRequest_GetNotAllowed );
}
HttpResponseBase response = context.HttpContext.Response;
if( !String.IsNullOrEmpty( ContentType ) )
{
response.ContentType = ContentType;
}
else
{
response.ContentType = "application/json";
}
if( ContentEncoding != null )
{
response.ContentEncoding = ContentEncoding;
}
if( Data != null )
{
JavaScriptSerializer serializer = new JavaScriptSerializer() { MaxJsonLength = MaxJsonLength, RecursionLimit = RecursionLimit };
response.Write( serializer.Serialize( Data ) );
}
}
}
并将其用作
return new LargeJsonResult { Data = Your Data, JsonRequestBehavior = `System.Web.Mvc.JsonRequestBehavior.AllowGet };`
您收到的错误是什么?如果您收到的是
HTTP500
,则表示您的应用程序出现异常。请提供详细信息。我希望您知道,在context.MyTable.ToList()中从p执行var r=from意味着您将整个表加载到内存中,然后返回一个子集,而不仅仅是向数据库请求前x行……我认为它与MVC没有任何共同之处。