Products Technologies Demo Docs Blog Support Company

Preparing ASP.NET for Long JSON Requests

The ASP.NET MVC DocumentViewer uses a Controller to manage incoming requests from the client using JSON. The default size of the supported JSON can be too small for handling larger files. This article shows how to use Json.NET to deserialize controller payloads.

Preparing ASP.NET for Long JSON Requests

The ASP.NET MVC DocumentViewer uses an ASP.NET controller to manage incoming client requests using JSON. The default JSON size of 4MB can be too small for handling larger files. This article shows how to replace the default JavaScriptSerializer with Json.NET.

In your ASP.NET Web Application, install the popular and de-facto standard Newtonsoft.Json:

Install-Package Newtonsoft.Json

Then create a class JsonDotNetValueProviderFactory derived from ValueProviderFactory:

public sealed class JsonDotNetValueProviderFactory : ValueProviderFactory {
    public override IValueProvider GetValueProvider(ControllerContext controllerContext) {
       if (controllerContext == null)
          throw new ArgumentNullException("controllerContext");

       if (!controllerContext.HttpContext.Request.ContentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase))
          return null;

       var reader = new StreamReader(controllerContext.HttpContext.Request.InputStream);
       var bodyText = reader.ReadToEnd();

       return String.IsNullOrEmpty(bodyText) ? null : new DictionaryValueProvider<object>(
          JsonConvert.DeserializeObject<ExpandoObject>(bodyText, new ExpandoObjectConverter()), CultureInfo.CurrentCulture);
    }
 }

In the Global.asax.cs file of your ASP.NET application, add the following code:

ValueProviderFactories.Factories.Remove(
  ValueProviderFactories.Factories.OfType<JsonValueProviderFactory>().FirstOrDefault());

ValueProviderFactories.Factories.Add(new JsonDotNetValueProviderFactory());

This code removes existing JsonValueProviderFactory objects and activates the newly created factory.

The complete Global.asax.cs of a new project should look like this:

public class MvcApplication : System.Web.HttpApplication {
  protected void Application_Start() {
    AreaRegistration.RegisterAllAreas();
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);

    ValueProviderFactories.Factories.Remove(
      ValueProviderFactories.Factories.OfType<JsonValueProviderFactory>().FirstOrDefault());

    ValueProviderFactories.Factories.Add(new JsonDotNetValueProviderFactory());
  }
}

After this change, the ASP.NET controllers use Json.NET to deserialize incoming payloads and can handle unlimited file sizes loaded into the DocumentViewer.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

ASP.NET

Integrate document processing into your applications to create documents such as PDFs and MS Word documents, including client-side document editing, viewing, and electronic signatures.

ASP.NET Core
Angular
Blazor
JavaScript
React
  • Angular
  • Blazor
  • React
  • JavaScript
  • ASP.NET MVC, ASP.NET Core, and WebForms

Learn more Trial token Download trial

Related Posts

ASP.NETJSON

Adjusting the Maximum Request Length for ASP.NET Core and ASP.NET Applications

Learn how to adjust the maximum request length for ASP.NET Core and ASP.NET applications to allow loading large files into the Document Viewer. This article shows how to configure the maximum…


ASP.NETJSONTables

How to Detect and Extract Table Data as JSON from PDF Documents in C#

A common use case is to extract tabular data from tables in a PDF document to create a structured and reusable data format. This article describes how to convert tables recognized by the PDF…


ASP.NETWindows FormsWPF

Generating Hierarchical Tables from JSON Data in .NET C#

Using TX Text Control, you can generate complex hierarchical tables directly from JSON data. This article explains the code and logic behind it.


AngularASP.NETDocumentViewer

Preview: TX Text Control DocumentViewer Becomes a Web Component

The next version of the TX Text Control DocumentViewer for ASP.NET (Core) and Angular is becoming a Web Component that can be easily used directly in JavaScript. This article gives an overview of…


ASP.NETASP.NET CoreDocumentViewer

Electronic Signatures: Requesting Signatures from Multiple Signers using…

This sample shows how to request signatures from multiple signers by using custom signature workflows and routing signature requests to custom endpoints.