Monday, February 18, 2013

Nancy.Json.JsonSettings.MaxJsonLength Exceeded

I was working on the web app which reads data from MongoDB and return the results to the UI as Jason using nancy. However I experienced the following error while returning a large chunk of data:-

Nancy.Json.JsonSettings.MaxJsonLength Exceeded

The data returned was so huge that it exceeded the max jason length (102400). The good news is that you can increase the max json length so that it caters for large amounts of data, this is how you can do this using c# :-

 JsonSettings.MaxJsonLength = Int32.MaxValue;

The above code will increase the max length from 102,400 to 2,147, 483, 647

A potentially dangerous Request.Path value was detected from the client (&).

I came across the the following error while testing one of company's web app:


A potentially dangerous Request.Path value was detected from the client (&).
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: A potentially dangerous Request.Path value was detected from the client (&).


Well the error is self-explanatory, my URL contained an illegal character (&). An alternative to resolve this error is through the use of  "requestPathInvalidCharacters " in your web.config file. You can implement this as follows:-

  <system.web>
    <httpRuntime requestPathInvalidCharacters="&lt;,&gt;,*,%,:,\" />
  </system.web>
 
The above should solve the problem. You can either choose to lock this down to a particular location, as recommended by one of the members of Stack Overflow:-


<location path="the/path/you/need/to/lock/down">
    <system.web>
        <httpRuntime requestPathInvalidCharacters="&lt;,&gt;,*,%,:,\"/>
    </system.web>
</location>
The above approach is even more safe because you only locking it down to a particular location, rather than exposing this to the whole site.


Regards,
Mpho