June 25, 2013

ASP.NET MVC 4: Changing the Maximum Upload and Download JSON Sizes

Post by: Dan Lorenz

With the creation of ASP.NET MVC, making AJAX calls is now easier than ever. The only problem is that Microsoft imposed a hard limit on the amount of data that can be uploaded or downloaded. These limits are probably more than enough for smaller websites, but when you start returning information on hundreds of products, the amount of data needed begins to rise quickly. Unfortunately, ASP.NET MVC 4 makes changing these maximum sizes more difficult than it needs to be. You would think making a simple web.config change is all you’d need to do, but developers need to do a lot more than that!

In order to change the upload and download sizes, developers have to make changes in two completely different places. To change the upload maximum, you have to remove a ValueProviderFactory and add your own. To change the download maximum, you have to inherit the normal Controller class and override the Json methods in addition to creating a new JsonResult class. (See our software development solutions)

First, I’ll cover how to change the maximum upload size. Keep in mind that IIS will still have the final say in the maximum file size. For instance, if you change MVC to support 100 MB files and IIS only supports 10 MB files, then the user can only upload 10 MB before an error occurs. The first step is to remove the original configuration. You can do this in the Application_Start method of the global.asax file. The code will look like this:

Now that the original provider is gone, we have to create and add its replacement. You will need to create a new class that inherits from ValueProviderFactory. You can name it something like MyValueProviderFactory. Here is the code you will need for it:

After creating this class, we have to add it back into the Factory list. You should add this code after removing the old one. It’ll look like this:

In this example, it will set the max size to the maximum Int32 value or about 2 GB.

Finally, I’ll cover how to change the maximum download size for a client. First, you’ll need to create a new class to hold your JSON data. It will inherit from JsonResult:

Next, you’ll need to create a new class that inherits from Controller. All of your existing controllers will have to inherit from this new class instead of the old Controller (a good practice, anyway). Once created, there are two methods you need to override. The code will look something like this for a class named MyControllerBase:

Now you have full control over the maximum upload and download JSON sizes! You can even change the values to be read from the web.config file, if you need to.

Relevant Insights

5 Steps to Reduce Your Ransomware Risk

As the recent ransomware attack on the U.S.’s second-largest meat producer, JBS, made clear, cyberattacks on critical infrastructure can cause...

How to Unlock the Organizational Value of Digital Transformation

As organizations look to stay competitive in today's dynamic and unpredictable marketplace, a trend has re-emerged that is ushering us...

When Is Your App Too Complex for Power Apps?

When Microsoft first launched Power Apps and Power Automate, these apps were positioned to replace legacy SharePoint on-premises functionality such...