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.

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

Are You Falling into the Cloud Conversation Gap?

Most conversations about migrating services to the cloud focus on migration of application workloads and data. The vision is often...

How Aggregation Tables Improve Performance for Power BI Reports

In this blog, Marcus Radue, Solution Architect, offers high-level guidance in the advantages of aggregation tables in Power BI. For a...

Getting Started with Azure DevOps – Views

Like most Microsoft products, there are many ways to look at or perform the same activities. Azure DevOps is no...