I have an ASP.NET / MVC4 application that uses jquery / ajax.
I'm trying to send a very large string from the client to the server using $.ajax( ... )
Primarily, we use the contentType "application/json"
for this which works perfectly. In this one particular case however, the server throws an exception because the data being transmitted is too long. I've tried absolutely everything to increase the maxJsonLength
for the deserializer in the web.config file but it doesn't work and no one can figure out why.
Someone suggested, as a work around, to send the contentType as "application/x-www-form-urlencoded; charset=UTF-8"
and then have my controller manually deserialize the object rather than letting the MVC framework do it.
Javascript:
function AjaxRequest(data, dataType, type, url, contentType, success, error, args)
{
if (url.indexOf("MyController/SomeMethodInMyController") > 0)
contentType = "application/x-www-form-urlencoded; charset=UTF-8";
data = JSON.stringify(data);
$.ajax({async: args.async, cache: args.cache, data: data, dataType: dataType, type: type, url: url, contentType: contentType, traditional: true, headers : { ... }, beforeSend: { ... }, success: { ... }, error: { ... }, complete: { ...});
}
function SomeFunction()
{
var object = {};
object.Name = $("#someControl").val();
object.RtfData = $("someOtherControl").val();
AjaxRequest(object, 'html', 'post', 'MyController/SomeMethodInMyController', 'application/json;', function (response) { ... }, function (error) { ... });
}
In this case, my application no longer crashes "interally" where the MVC framework attempts to deserialize the object on its own. Now it bypasses all that and directly calls the method in my controller. Kind of hacky, but 3 days later I'll take what I can get.
C#:
public void SomeMethodInMyController(string formData)
{
JavaScriptSerializer jss = new JavaScriptSerializer();
jss.MaxJsonLenght = int.MaxValue;
MyType objMyType = jss.Deserialize<MyType>(formData);
//do stuff with objMyType
}
Problem is that when I set a breakpoint in this method, formData
is null
.
In my browser's console, before $.ajax();
is actually executed I typed typeof(data)
into the console which returns "string"
. If I mouse over the symbol I can see all the data I expect it to contain is there. So why is that in my C# code the value is null
?