MVC3:Maximum request length exceeded - asp.net-mvc-3

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.

Related

IIS Bad Request for the site configured with url-rewrite with ARR

I have website created under IIS 8.0 in Windows 2012. An URL rewrite with ARR has created under this site which points to a linux machine (Basically to a webservice deployed in Tomcat) and through IIS there will be "GET" request where the URL is exceeding more than 5000 characters along with query string. When the URL is hit through program or in browser, I see IIS throws "Bad Request" with Status code as 400 and no Substatus code. The same works when it is directly hit to the webservice (for tomcat Linux). I suspected the issue is because of the excess characters in the url, because when I try decreasing the url length to 3500 characters which works without any error. Below are the configuration and settings that I have tried in IIS web.config and as well as http.sys registry, but nothing seems to be work.
<configuration>
<system.webServer>
<security>
<requestFiltering allowDoubleEscaping="true">
<requestLimits maxAllowedContentLength="4294967295" maxUrl="10999" maxQueryString="2097151">
<headerLimits>
<add header="Content-Type" sizeLimit="100000000" />
</headerLimits>
</requestLimits>
</requestFiltering>
</security>
<urlCompression doDynamicCompression="false" />
</system.webServer>
<system.web>
<httpRuntime maxRequestLength="8192" maxUrlLength="8192" maxQueryStringLength="8192" requestPathInvalidCharacters="" />
</system.web>
</configuration>
http.sys Registry Settings
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\HTTP\Parameters\MaxFieldLength - DWORD - 65534
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\HTTP\Parameters\MaxRequestBytes - DWORD - 16777216
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\HTTP\Parameters\UrlSegmentMaxCount - DWORD - 16383
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\HTTP\Parameters\UrlSegmentMaxLength - DWORD - 32766
I have rebooted the Windows server after every above settings and configurations was done.
These settings and configuration are honored by IIS without url rewrite. If I try other url with length of 5000 characters for the same website its works without any error. This is appearing only for the url that has fallen under URL rewrite config. Is there any specific configurations that need to be done with respect to URL length in ARR apart from the above mentioned? Please suggest and help.
Thanks in Advance.
Answering to my question.
The issue was on the Linux Tomcat side not on IIS, the catalina.out logs was stated as below
INFO: Error parsing HTTP request header
Note: further occurrences of HTTP header parsing errors will be logged at DEBUG level.
java.lang.IllegalArgumentException: Request header is too large
I added the configuration of maxHttpHeaderSize="65536" in server.xml of tomcat/conf. After modifying this, the issue was resolved.

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

WebApi 2 Maximum request length exceeded

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>

mvc3 error on webserver, only get generic response

Edit: showed my exact web.config code.
I have a MVC3 project that works fine on my box, but when I upload it to the web server, it gives an error on a certain page. I am trying to determine the exact error, but it keeps redirecting to the "Error/ShowError" action. I tried modifying the web.config file to say showcustomerrors=false, but it still redirects. I really need to see the actual error in order to troubleshoot the problem.
In firebug, it shows that the error is a 500 internal server error. I haven't been able to get any more detailed than that.
Also, if I run the page from my local box, but use the remote database, I don't get an error. This makes me think it's related to directory permissions.
Here is in my web.config section:
<compilation debug="true" targetFramework="4.0">
Here is in my web.debug.config section:
<customErrors mode="Off">
Thanks!
See if these changes will help.
<configuration>
<system.webServer>
<httpErrors errorMode="Detailed" />
</system.webServer>
<system.web>
<customErrors mode="Off"/>
<compilation debug="true"/>
</system.web>
</configuration>
If not, make sure that AspNet is properly installed.
C:\Windows\Microsoft.Net\Framework..\v.....\aspnet_regiis.exe -i
Edit:
You can try to log the exception message using the following method in Global.asax.cs
protected void Application_Error(object sender, EventArgs e) {
var exception = Server.GetLastError();
// Log the exception
}
Edit:
Well, this will be a lot to do, but I suggest you to add Elmah to your project to log unhandled exceptions. See the first step on Logging in MVC Part 1- Elmah
Elmah is available on NuGet Gallery.
Turn off customErrors in web.config and you will be able to see what the error is. then fix it. Turn the Custom Error on
<customErrors defaultRedirect="Error.aspx" mode="Off"/>
Here are the options you have for custom errors:
<customErrors defaultRedirect="url"
mode="On|Off|RemoteOnly">
<error. . ./>
</customErrors>
I think your issue is that you're using 'false' instead of 'Off'.
Good luck, hope this works for you.
Reference: MSDN link for CustomErrors section

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>

Resources