javascriptc#razorasp.net-mvc-5web-config

ASP.NET MVC5: Change Upload Limit for Specific URL


I have a partial View page (ManageFile.cshtml) which is being used to upload and download files based on the current page and id. Since this View is partial, I am able to utilize it throughout my project, on various different pages, which makes it fairly convenient to implement and record. At this point I am not considering any changes in the way files can be uploaded or downloaded. It is really ingrained in the project's structure.

My question is: How can I change the file upload size limit on one specific View (that contains this partial View) through the web.config file (if possible at all)? The default maxRequestLength and maxAllowedContentLength are 20 MB, but I want to increase these limits to 100 MB for this one specific page.

Note: I can modify scripts that checks the file size and make restrictions there, but I would like to only modify the web.config file in case any changes needs to be made and I would only need to modify one file.

In the web.config file, I tried adding a location tag and specifying the path to be like so below (note the between the maxRequestLength and maxAllowedContentLength)

<!-- Upload settings for specific page -->
<location path="~/Project/Details.cshtml">
    <system.web>
        <httpRuntime targetFramework="4.7.2" maxRequestLength="102400" executionTimeout="600" />
    </system.web>
    <system.webServer>
        <security>
            <requestFiltering>
                <requestLimits maxAllowedContentLength="10485760" />
            </requestFiltering>
        </security>
        <validation validateIntegratedModeConfiguration="false" />
    </system.webServer>
</location>

<!-- Default upload settings -->
<system.web>
    <httpRuntime targetFramework="4.7.2" maxRequestLength="20480" executionTimeout="600" />
</system.web>
<system.webServer>
    <security>
        <requestFiltering>
            <requestLimits maxAllowedContentLength="2097152" />
        </requestFiltering>
    </security>
    <validation validateIntegratedModeConfiguration="false" />
</system.webServer>

When I tried uploading a file using these settings, I was able to confirm that page did have the changed request limit. However, when I tried calling the upload method (located in a different controller file), the page would quickly refresh and the changed request limit would go back to the default values, thus causing an error.

Edit: I understand you can restrict the upload size through some frontend scripts or some coding in the controller method, but I was just wondering if there is a solution via only configuring the web.config.


Solution

  • It seems like there is no way to only modify the web.config file to change the upload size limit for a specific page given that my implementation of my file upload method is used through a partial view which is already being utilized throughout the project.

    This method may work if I had an separate completely dedicated file upload/download implementation for the specific page, but that is a question for another day. My question has been answered.