Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop processing if JSON is too large

I want to be able to stop processing a request if the JSON that is posted to my controller's action is too large.

The size limit will vary so I don't want to hard code this value in my web.config

Is there a way to check in my controllers action? Say my limit is 100kb, if it goes over I don't want to waste the servers CPU, I just want to halt and return right away.

like image 364
cool breeze Avatar asked Oct 29 '15 19:10

cool breeze


1 Answers

There is such a setting for this in the Json class (See Code Below):

var jsonResult = Json(jsonString, JsonRequestBehavior.AllowGet);
jsonResult.maxJsonLength = int.MaxValue;
return jsonResult;

It can also be done in configuration like this (Web.config):

<configuration> 
  <system.web.extensions>
   <scripting>
       <webServices>
           <jsonSerialization maxJsonLength="50000000"/>
       </webServices>
   </scripting>
  </system.web.extensions>
</configuration> 
like image 168
Mr. B Avatar answered Sep 30 '22 21:09

Mr. B