출처 - http://brianreiter.org/2011/01/03/custom-jsonresult-class-for-asp-net-mvc-to-avoid-maxjsonlength-exceeded-exception/
LargeJsonResult ActionResult class
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051using
System;
using
System.Web.Script.Serialization;
namespace
System.Web.Mvc
{
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 ) );
}
}
}
}
You can use return new LargeJsonResult(){ Data = data } from any Action method where you would have used return Json(data). Also, you have direct control over the MaxJsonLength and RecursionLimit properites of JavaScriptSerializer.
1return
new
LargeJsonResult() { Data = output, MaxJsonLength =
int
.MaxValue };
'[ Web ] > ASP.NET MVC' 카테고리의 다른 글
Razor문법 - Quick Reference (0) | 2012.12.04 |
---|
댓글