Tuesday, August 06, 2013

Issue
If you are using IIS Web server for hosting your application, Sometimes these kind of error can be trigger when try to upload a large file using your application.
  • "The request filtering module is configured to deny a request that exceeds the request content length."
  • "System.Web.HttpException: Maximum request length exceeded."
Reason
Default IIS settings only allow to upload up to  4Mb large files. Rest are filtered and shows the one of above error.

Solution
Solution is you need to increase default max allow file size to what ever you want size. This can be do in two levels
  1. Application level
  2. Server level
 Lets see how to do each. first we are going to look at "Application Level" configuration changes.

1. Application Level
Step1:
Go to  "Control Panel" -> "Administrative Tools" ->" Internet Information Services (IIS) Manager"

Step2: 
Expand the connection explorer and double click on the site you working on.

Step 3:
In the right hand panel, you can see the set of icons under few categories, Go to category Management and double click on the "configuration editor icon". 


Step 4:
Then the configuration editor window will appear and you have to click the link "Search Configuration" at right hand side pane.


Step 5: 
Then "Configuration Search" dialog will appear and first you have to select the "web.config" file and then click the link on right bottom of the dialog.



Step 6:
Then "web.config" file will open in your default XML editor and it shows all the configuration that applicable to particular web site. So now we are going to add default upload limit configuration to MySite01, to override default server configuration.Here I need to allow file upload until 1GB (1*1024*124 = 1048576 KB) size. So what i need to do is just copy and paste following piece of code to the "web.config" file just after start the <configuration> tag.

<system.webServer>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="2147483648"/>
      </requestFiltering>
    </security>
  </system.webServer>
  <system.web>
    <httpRuntime maxRequestLength="1048576" />
  </system.web>




Step 6: 
Then your file should looks like this. Be careful someone may be previously added the same configuration to this file. At that situation you have to edit the values instead of newly adding.



Step7: 
Now you edit the configuration. But to apply these configurations you have to restart the IIS server. To do so run the command prompt as Administrator (click start button-> type cmd in search box -> once you see the command prompt icon -> right click and select "Run as administrator"). Type iisreset and press enter.


2. Server Level

Setp 1:
Open the command prompt as administrator (click start button-> type cmd in search box -> once you see the command prompt icon -> right click and select "Run as administrator") and go to c:\Windows\System32\inetsrv directory

Step 2:
 Type the following command and press enter

appcmd.exe unlock config -section:system.webServer/security/requestFiltering

Step 3:
If your command is correct and worked properly you can see a result like following window. Actually here disabling the file size validation. But not, increasing the allowed max file size.

Step 4:
Now you edit the configuration. But to apply these configurations you have to restart the IIS server. Type iisreset and press enter.

6 comments:

  1. Thank you very much this was very helpful in a production environment !
    Sincerely ,Aline

    ReplyDelete
  2. THANK YOU SO SO MUCH...I MUST BUY YOU LUNCH

    ReplyDelete
  3. Thanks a lot. It helped me a lot.

    ReplyDelete
  4. Thank you very much. You did a wonderful job with this post. This saved me a lot of time. I owe you Lunch too.

    ReplyDelete
  5. This is an amazing guide. Thank you so much.

    ReplyDelete