WebApi 2 Maximum request length exceeded - asp.net-web-api

Does anyone know if there is a way of catching this error?
Essentially I'm trying to implement some functionality to allow a user to upload a file from a webpage to a webapi controller.
This works fine, but if the file size exceeds the maximum size specified in the web.config the server returns a 404 error.
I want to be able to intercept this and return a 500 error along with message which can be consumed by the client.
I can't work out where to do this in WebApi as the Application_Error method I've implemented in Global.asax is never hit and it seems like IIS is not passing this through to the WebApi application.

Try to set IIS to accept 2GB requests(in Bytes).
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="2147483648" /> // 2GB
</requestFiltering>
</security>
</system.webServer>
And set reasonable request size for ASP.NET app(in kiloBytes).
<system.web>
<httpRuntime maxRequestLength="4096" /> // 4MB (default)
</system.web>
Now IIS should let requests less than 2GB pass to your app, but app will jump into Application_Error reaching 4MB request size. There you can manage what you want to return.
Anyway, requests greater than 2GB will always return 404.13 by IIS.
Related links:
Dealing with large files in ASP.NET Web API

How it looks like on my server:
<system.web>
<httpRuntime targetFramework="4.6.1" maxRequestLength="16240" />
</system.web>

Related

.Net Core 2.1 Error 413 on multipart/form-data POST only on IIS not IIS Express

I'm hitting a problem with multipart/form-data POST uploads on IIS. My client is an Angular SPA and my backend is on .Net Core 2.1 (I know it's old).
The backend project is published as Self-Contained win-x64. I'm not sure how it's configured exactly on IIS / Kestrel but the IIS App runs under a specific Application Pool (No managed Code / Integrated). My web.config looks like this:
<configuration>
<location path="." inheritInChildApplications="false">
<system.web>
<httpRuntime maxRequestLength="1048576" />
</system.web>
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="1073741824" />
</requestFiltering>
</security>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath=".\my.app.exe" stdoutLogEnabled="false" stdoutLogFile=".\log\path" />
</system.webServer>
</location>
</configuration>
In my development environment instead I'm using IIS Expres.
Now I added a multipart/form-data upload, sending form data together with a blob/image. This worked out of the box in development settings. However when I publish to staging environment with real IIS and the above web.config, I always get 413 Request entity too large.
My controller looks like this:
[HttpPost]
[DisableRequestSizeLimit]
[RequestFormLimits(MultipartBodyLengthLimit = int.MaxValue, ValueLengthLimit = int.MaxValue)]
[RequestSizeLimit(int.MaxValue)]
[Route("my/route")]
public ActionResult MyHandler()
I also added limits for Kestrel in Program.cs:
.UseKestrel(options =>
{
options.Limits.MaxRequestBodySize = 104857600; // 100MB
}
)
And to make the weirdness complete the 413 in staging environment only happens in Firefox. I have no idea what else I can do. I also cleared cache in firefox.
After longer search I finally found the necessary setting in IIS to make this work in Firefox. And it has indeed be mentioned in a few sources as the 'last option'. For me it was necessary in this case.
In 'IIS Manager' I selected the backend application and opened 'Configuration Editor'
system.webServer/serverRuntime -> uploadReadAheadSize=2147483647
That maked it work.

Maximum size Exceeded exception while uploading mp4 file to web api in mvc

I am trying to upload mp4 format file to web api from rest sharp.But every time it gives me Maximum file size exceeded exception.I Put
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="1073741824" />
</requestFiltering>
</security>
this code in my web api application web config file but no result.
I am calling my web api from other application in that I use Nuget RestShap to call web api.Please help me.
Code to call web api.
var request = new RestRequest("Uploads", Method.POST);
request.AddFile("filename", Server.MapPath("/Images/videoplayback.mp4"), "multipart/form-data");
request.AddQueryParameter("participantsId", "2");
request.AddQueryParameter("taskId", "77");
request.AddQueryParameter("EnteredAnswerOptionId", "235");
request.AddQueryParameter("lat", "12.15");
request.AddQueryParameter("lon", "12.56");
request.AlwaysMultipartFormData = true;
IRestResponse response = createClient().Execute(request);
Also add this entry or add the attributes if you already have this entry in your web.config. The executionTimeout is in seconds and the maxRequestLength is in KB. Example is 2 hours and 1GB
<system.web>
<httpRuntime executionTimeout="7200" maxRequestLength="1048576" />
</system.web>
I think you should have to refer these links,you may get required stuff here...
http://www.strathweb.com/2012/09/dealing-with-large-files-in-asp-net-web-api/
https://social.msdn.microsoft.com/Forums/sqlserver/en-US/b5dbab38-9741-45ca-a791-1847b2935bb0/azure-api-app-rest-web-api-upload-limit?forum=AzureAPIApps

Getting No 'Access-Control-Allow-Origin' when sending large JSON via AJAX

I'm working on a web page that calls a REST webservice via ajax to get and insert data.
The problem is that we need to send a base64 image in a JSON. You know, the base64 image is the imaged converted to that large text: base64/fjhd7879djkdadys7d9adsdkjasjdshk...
When we try with a 1 KB image, it works.
But with a bigger file(55kb), it doesn't.
So I assume it has something to do with the maxRequest, but the error says that is No 'Access-Control-Allow-Origin'. But we havent fount any way to configure it. Please help.
By default browsers block json requests from other domains other than the page unless the json request has the Access-Control-Allow-Origin header, so you'll need to add that header to your json requests on that service or use the same domain for both.
More info here: https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS
You can try setting the maxJsonLength to it's maximum value in the web.config file.
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="2147483647"/>
</webServices>
</scripting>
</system.web.extensions>
I know this is an old post, but for anyone who still might be having this problem, I solved it by adding two settings to Web.config as described here: https://west-wind.com/webconnection/docs/_4lp0zgm9d.htm
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="2147483647"></requestLimits>
</requestFiltering>
</security>
<!--snip-->
</system.webServer>
and
<system.web>
<httpRuntime maxRequestLength="2147483647" />
<!--snip-->
</system.web>

JQuery Ajax File Upload Fail on Large Files

I currently have a ASP.Net MVC web application that needs to upload large files using ajax. I am currently using this jQuery plugin - http://valums.com/ajax-upload/. I have also used this plugin - http://jquery.malsup.com but get the same result.
The issue that I am having for large file is that the iframe that gets generated to in order for the request to be asynchronous is not loading in time.
It always seems to point to this code:
var doc = iframe.contentDocument ? iframe.contentDocument : iframe.contentWindow.document, response;
For smaller files the script works great but for larger files the iframe nevers seems to get initialized properly.
This has been driving me crazy. Can someone please HELP.
thanks in advance
You might need to increase the maximum allowed request size on the server as well as the execution timeout of the request using the <httpRuntime> section in your web.config
<system.web>
<httpRuntime
maxRequestLength="size in kbytes"
executionTimeout="seconds"
/>
...
</system.web>
And if you are deploying your application in IIS 7.0+ you might also need to increase the maximum allowed request size using the <requestLimits> node of the <system.webServer> section:
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="size in bytes" />
</requestFiltering>
</security>
...
</system.webServer>

MVC3:Maximum request length exceeded

Here is the context:
ASP MVC3 - IIS 7
I am using ElMah configured very well as documented nearly everywhere for MVC.
And it is working.
Here is what I have in my config file
<system.web>
<httpRuntime maxRequestLength="1024" enableVersionHeader="false" />
<customErrors mode="On" defaultRedirect="~/Error/Generic" />
...
<system.webServer>
<httpErrors errorMode="Detailed" existingResponse="PassThrough" defaultPath="~/Error/Generic" defaultResponseMode="ExecuteURL"></httpErrors>
Now here is my problem
I spent the whole day trying to catch this error : "Maximum request length exceeded" when I submit a file that exceed 1024 bytes
Between asp errors and IIS errors and sometimes IE interception, I'am getting crazy
I cannot understand why this is so complicated.
I already tried these solutions and none worked for me
solution 1
Solution 2
Thank you for your help
When setting maxRequestLength to 1024 this means 1024 kilobytes, not 1024 bytes. Maybe this is why the exception isn't thrown.

Resources