4

I have my ASP.NET WebAPIs hosted on IIS 7.5+ which has default Max allowed content length set to 30000000 Bytes.

When I am opening Request Filtering feature of my webAPI on IIS and check the entry there, it says 30000000 Bytes, but when I am doing upload for files larger than 4MB to the webAPI it is throwing exception as below.

Message "Maximum request length exceeded."

Stack Trace:

at System.Web.HttpBufferlessInputStream.ValidateRequestEntityLength() at System.Web.HttpBufferlessInputStream.GetPreloadedContent(Byte[] buffer, Int32& offset, Int32& count) at System.Web.HttpBufferlessInputStream.BeginRead(Byte[] buffer, Int32 offset, Int32 count, AsyncCallback callback, Object state) at System.IO.Stream.b__d(Stream stream, ReadWriteParameters args, AsyncCallback callback, Object state) at System.Threading.Tasks.TaskFactory`1.FromAsyncTrim[TInstance,TArgs](TInstance thisRef, TArgs args, Func`5 beginMethod, Func`3 endMethod) at System.IO.Stream.BeginEndReadAsync(Byte[] buffer, Int32 offset, Int32 count) at System.IO.Stream.ReadAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken) at System.Web.Http.WebHost.SeekableBufferedRequestStream.d__0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at System.Net.Http.HttpContentMultipartExtensions.d__8.MoveNext()

Files smaller than 4MB are getting uploaded fine.

Any idea what places I should be looking for to see where this limit is set?

What I have Tried:

I have checked Server's Web.config, machine.config as well as all the application's web.config, but don't see any entry for size limit anywhere.

I have also tried setting up the entries in webAPI's web.config - maxRequestLength and maxAllowedContentLength files as mentioned in various other questions that ask for increasing the size limit for file upload. On doing that Request Filtering Feature in IIS was showing me new value but I was still getting the same exception for files larger than 4MB.

Guanxi
  • 3,103
  • 21
  • 38
  • Have you tried yo set ``? – Reza Aghaei Dec 02 '15 at 19:15
  • I think this answer might be helpful: http://stackoverflow.com/a/30870507/1718624 – jonfriesen Dec 02 '15 at 19:19
  • How about this: http://stackoverflow.com/questions/15506648/how-to-upload-a-large-file-with-asp-net-mvc4-web-api-with-progressbar – lcryder Dec 02 '15 at 19:21
  • Yes, I tried setting that in config file, after setting that Request Filtering feature was showing me the updated value, but still I was getting the same error. – Guanxi Dec 02 '15 at 19:49
  • Are you sure you have no other web.config files in you site? – Alexei Levenkov Dec 02 '15 at 21:36
  • Also make sure you are changing the config in correct file. For more information about asp.net configuration file, you can see [ASP.NET Configuration File Hierarchy and Inheritance](https://msdn.microsoft.com/en-us/library/ms178685.aspx) – Reza Aghaei Dec 02 '15 at 23:54

1 Answers1

3

As stated here you should set both maxRequestLength and maxAllowedContentLength.

You can set maxRequestLength in web.config this way:

<system.web>
    <httpRuntime maxRequestLength="size in kilo bytes" />
</system.web>

This setting specifies the limit for the input stream buffering threshold, in KB. This limit can be used to prevent denial of service attacks that are caused, for example, by users posting large files to the server.

The default is 4096 (4 MB).

Also you can set maxAllowedContentLength in web.config this way:

<system.webServer>
    <security>
        <requestFiltering>
            <requestLimits maxAllowedContentLength="size in bytes" />
        </requestFiltering>
    </security>
</system.webServer>

Specifies the maximum length of content in a request, in bytes.

The default value is 30000000.

Also make sure you are changing the config in correct file. For more information about asp.net configuration file, you can see ASP.NET Configuration File Hierarchy and Inheritance

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • 1
    Thanks for response, I have updated my question. I have already tried this and it is not working for me. – Guanxi Dec 02 '15 at 19:54
  • Have you set both of them? – Reza Aghaei Dec 02 '15 at 19:58
  • Yes I setup both of them. – Guanxi Dec 02 '15 at 19:58
  • I tried this on my local and it worked, but on hosting environment, this is not working. I have verified all the web.configs and none of them have any entry. I am making entry to increase maxRequestLength in my webapi's web.config. I am using a SPA to call into my webAPI. should that SPA app's web.config also needs this entry. – Guanxi Dec 03 '15 at 14:41
  • Depending on how you implemented the SPA application you may need to apply that settings to the web.config of your SPA application too. If your SPA application is totally a client application, you don't need that setting. As an example if you pass the file directly to the API from client, you don't need that setting. But for example if your SPA application for example first upload the file on server and then you use that API to store uploaded file, you need that setting in web config of your SPA application too. – Reza Aghaei Dec 03 '15 at 15:02
  • could you overcome the problem? – Reza Aghaei Dec 04 '15 at 21:18
  • 1
    Sorry @Reza, I am still not able to overcome this issue. My SPA is not coming in picture in file upload but will still try to see if I am missing something. Also, my SPA is hosted separately than my webAPI, so there won't be inheritance of config entries. By the way, your answer specifying the two default limits clears some confusion on my part. – Guanxi Dec 07 '15 at 13:42
  • @Guanxi Thank you for your feedback. Hope you can solve the problem soon. – Reza Aghaei Dec 07 '15 at 13:51
  • @Guanxi **Also, my SPA is hosted separately than my webAPI, so there won't be inheritance of config entries.** When uploading file, does the target of the post is your SAP actions or your API action? (If the target of post is your SPA Application actions, you should apply the size limit on both SPA Application configs and API configs.) – Reza Aghaei Dec 07 '15 at 13:53