Passing a very large json object to mvc4 controller - json

I've had a look and I can't seem to get this to work and I constantly get the error message that my json object is too large to pass to my controller.
The length of the string exceeds the value set on the maxJsonLength property.
So I have a json object that contains the base64 string of up to 3 images as well as some textual data.
I am trying to pass that object to the controller. It works fine if I do not include the images.
So I have tried, in web.config;
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="500000000"/>
</webServices>
</scripting>
</system.web.extensions>
as well as;
<add key="aspnet:MaxJsonDeserializerMembers" value="150000000" />
I have changed my controller from an ActionResult to a JsonResult.
I've read that I need to create my own json parser etc and a lot of the sites deal with passing data from the controller to the client.
I am trying to get a large amount of data from the client to the controller.
Any help here?

If you take a look at the source code ASP.NET MVC for the JsonResult class, you find this code (+):
if (Data != null)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
if (MaxJsonLength.HasValue)
{
serializer.MaxJsonLength = MaxJsonLength.Value;
}
if (RecursionLimit.HasValue)
{
serializer.RecursionLimit = RecursionLimit.Value;
}
response.Write(serializer.Serialize(Data));
}
The defualt values for MaxJsonLength property is set to 2MB and RecursionLimit is set to 100.
You can change the defualt values this way:
return new JsonResult
{
MaxJsonLength = Int32.MaxValue,
Data = customers,
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
more info.

Related

Passing json through a controller

I have this back-end system that produces json. Is it possible to pass that data through a controller on request and correctly setting the mimetype? Everything I've tried so far tries to re-serialize the data and thus escaping json string.
I've tried adding [Produces("application/json")] to the controller. Setting the response type: Response.ContentType = MediaTypeNames.Application.Json;, returning a JsonResult object: return new JsonResult(jsonString);
The back-end doesn't have a JsonSerializer I can add to the JsonSerializerOptions and I only need it for this specific controller.
In short: If I just return the json string as an IActionResult without doing anything the controller works except for the fact that the content-type isn't set to application/json. Is there any way to do this without affecting anything else?
I feel like this shouldn't be such a exceptional use case but I just can't seem to find the answer.
I had the same need. I solved it by having my action method return an ActionResult like so:
public ActionResult SomeActionMethod(){
string json = GetJsonFromBackenend();
return new ContentResult {
Content = json,
ContentType = "application/json; charset=utf-8",
StatusCode = StatusCodes.Status200OK
};
}
You could declare the return type as an IActionResult if you prefer as #Jurgy mentioned.

Solr custom query parser "fl" parameter not returning desired result

I am currently working on replacing an existing searching platform with Solr. I am new to Solr and currently using Solr Version 6.6.0. Legacy system accepts a JSON object with search parameters. Thus I was asked to implement a custom query parser which accepts a query string in JSON format.
Essentially NO other parameter is passed to Solr except "q" which contains all the required parameters encoded in a JSON formatted string. All the common parameters and local parameters required to execute the query is then extracted by the query parser itself.
This feat was successfully achieved except the "fl" parameter will not give the expecting results. Which means query result composes of all the fields that are present in the document. Code looks like this;
public static final String FIELD_LIST = "fl";
Collection for storing the key value pairs.
HashMap<String,Object> paramMap = new HashMap<String, Object>();
Decoding and formatting the field list.
String ifld = getFieldList();
Parameters are put in the collection thus.
paramMap.put(Constants.FIELD_LIST,ifld);
SolrParams are then generated.
return SolrParams.toSolrParams(new NamedList<>(paramMap));
Each and every query parameter is processed this way and finally in the query parsers parse() method i have this.
//calls the factory method to get the appropriate request type translator
JSONRequestTranslator jrt = (JSONRequestTranslator)JSONQParser.getTranslator(requestType);
//converts the arguments to solrparams.
qstr = jrt.getQueryString();
localParams = jrt.getLocalParams();
params = jrt.getParams();
req.setParams(SolrParams.wrapDefaults(params, this.req.getParams()));
return jrt.parse(qstr, localParams, params, req);
parse() method in classes implementing the JSONRequestTranslator interface looks like this
#Override
public Query parse(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) throws SyntaxError {
return new DisMaxQParser(qstr, localParams, params, req).parse();
}
This works fine but the "fl" parameter seems to have no effect. I don't have a default setting in solrconfig.xml file and while i'm debugging i always find the "fl" parameter set to correct value. I have tried several other approaches like writing custom search component plugins but please explain how to fix this and what am i missing here. Thank you.

'System.OutOfMemoryException' was thrown while loading bulk data

When I loaded 100,000 rows with 20 columns intp kendo grid, I am getting 500 error.
So i have checked the response of json , getting memory out of exception . This is the following code.
In the webconfig,have set
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="500000000"/>
</webServices>
</scripting>
</system.web.extensions>
This is the mvc controller.
public JsonResult JqueryKendoGridVirtualScrolling()
{
using (var s = new KendoEntities())
{
var x = s.premiumsbytreaties.ToList().Take(100000);
if (x != null)
{
var jsonResult = Json(x, JsonRequestBehavior.AllowGet);
jsonResult.MaxJsonLength = int.MaxValue;
return jsonResult;
//return Json(x.ToList(), JsonRequestBehavior.AllowGet);
}
else
{
return null;
}
};
}
It is working fine for 6 columns.But not working for 15 columns.
working fine with 20,000 thousand record,see the output
Two problems here:
500,000,000 characters is too long for the JSON content of an HTTP Response. This will take time to pass over the network and that is before the client browser can parse and process the data (taking time and memory).
1% of this is still too much.
You are not using Kendo's support for "Grid / Virtualization of remote data".
In their example the action's first parameter is [DataSourceRequest] DataSourceRequest request and then this is used (with the ToDataSource extension method) in creating the response.
Like with AJAX methods supporting Kendo Grids (and other controls)
these work together to pass paging, sorting and filtering parameters and
apply to the data before it is returned.
If your data is an IQueryable<T> for a suitable db provider this will
happen on the database.

Compress on code json string and return to client

I have a ASP.NET MVC Controller C# and inside I have the following method:
public JsonResult PlayersData()
{
List<Player> players = new List<Player>();
// here some code to fill players list
// ...
return Json(players, JsonRequestBehavior.AllowGet);
}
I would like to send the Json compressed, I mean the List because the json is heavy about 12MB.
I don't want to enable IIS compression, I need to do it within the same code.
Any clue?

Error The length of the string exceeds the value set on the maxJsonLength property

I am currently have a grid with PDF attachment. Each PDF can have file size up to 1MB. The problem is I am getting the value "Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property"
I already placed the following in the web.config but the problem is it works only if the Kendo UI Grid needs to display 6 records.
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength = "2147483647"></jsonSerialization>
</webServices>
</scripting>
</system.web.extensions>
Based on the following link:
MaxJsonLength exception in ASP.NET MVC during JavaScriptSerializer
public ActionResult SomeControllerAction()
{
var jsonResult = Json(veryLargeCollection, JsonRequestBehavior.AllowGet);
jsonResult.MaxJsonLength = int.MaxValue;
return jsonResult;
}