Geeks With Blogs
Fórmulas e Cenas Object Reference Not Set to an Instance of an Object

In global.asax:

 

protected void Application_BeginRequest(Object sender, EventArgs e)
{
	HttpRuntimeSection runTime = (HttpRuntimeSection)WebConfigurationManager.GetSection("system.web/httpRuntime");
	//Approx 100 Kb(for page content) size has been deducted because the maxRequestLength proprty is the page size, not only the file upload size
	int maxRequestLength = (runTime.MaxRequestLength - 100* 1024;
 
	//This code is used to check the request length of the page and if the request length is greater than 
	//MaxRequestLength then retrun to the same page with extra query string value action=exception

	HttpContext context = ((HttpApplication)sender).Context;
	if(context.Request.ContentLength > maxRequestLength)
	{
		IServiceProvider provider = (IServiceProvider)context;
		HttpWorkerRequest workerRequest = (HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest));
		// Check if body contains data
		if(workerRequest.HasEntityBody())
		{
			// get the total body length
			int requestLength = workerRequest.GetTotalEntityBodyLength();
			// Get the initial bytes loaded
			int initialBytes = 0;
			if(workerRequest.GetPreloadedEntityBody() != null)
				initialBytes = workerRequest.GetPreloadedEntityBody().Length;
			if(!workerRequest.IsEntireEntityBodyIsPreloaded())
			{
				byte[] buffer = new byte[512000];
				// Set the received bytes to initial bytes before start reading
				int receivedBytes = initialBytes;
				while(requestLength - receivedBytes >= initialBytes)
				{
					// Read another set of bytes
					initialBytes = workerRequest.ReadEntityBody(buffer, buffer.Length);
					// Update the received bytes
					receivedBytes += initialBytes;
				}
				initialBytes = workerRequest.ReadEntityBody(buffer, requestLength - receivedBytes);
			}
		}
		// Redirect the user to the same page with querystring action=exception. 
		context.Response.Redirect("~/Erros/FicheiroGrande.aspx");
	}
}

 

Source Posted on Tuesday, September 20, 2011 6:36 AM Geral , Asp.Net | Back to top


Comments on this post: How to handle ""Maximum request length exceeded" exception

# re: How to handle ""Maximum request length exceeded" exception
Requesting Gravatar...
Thanks,

You save my day!!
Left by Lyra on Nov 22, 2011 7:30 AM

# re: How to handle ""Maximum request length exceeded" exception
Requesting Gravatar...
Shouldn't the update of initialBytes:

initialBytes = workerRequest.ReadEntityBody(buffer, requestLength - receivedBytes);

be at the end of the while loop, but inside it?
Left by yhayes on Aug 15, 2012 10:49 AM

# re: How to handle ""Maximum request length exceeded" exception
Requesting Gravatar...
The Line initialBytes = workerRequest.ReadEntityBody(buffer, requestLength - receivedBytes); is outside the cicle to read the remaining bytes.
Left by Sérgio Lima on Aug 15, 2012 2:45 PM

# re: How to handle ""Maximum request length exceeded" exception
Requesting Gravatar...
Thank-you Sergio.
Another question/problem:
I still get the Maximum Request Length Error on the last statement which redirects to the error page. Any thoughts?
Left by yhayes on Aug 16, 2012 10:15 AM

# re: How to handle ""Maximum request length exceeded" exception
Requesting Gravatar...
It works great on local, but doesn't work on the production server and I'm not sure why to that.
Left by Kes on Dec 27, 2012 10:58 PM

# re: How to handle ""Maximum request length exceeded" exception
Requesting Gravatar...
Thanks a lot ! This surely saved my day !
Left by sumeet kumar on Feb 20, 2013 10:10 AM

# re: How to handle ""Maximum request length exceeded" exception
Requesting Gravatar...
for who are not working please add the above code to try catch block , this work for sure ..
Left by Akhilesh on Feb 24, 2014 9:46 AM

# re: How to handle ""Maximum request length exceeded" exception
Requesting Gravatar...
maybe add after while

if (requestLength > receivedBytes){...}
Left by mo on Jan 04, 2015 11:42 AM

# re: How to handle ""Maximum request length exceeded" exception
Requesting Gravatar...
<system.web>
<httpRuntime maxRequestLength="1073741800" targetFramework="4.5" />
</system.web>
Left by Sumit on Feb 19, 2018 6:23 AM

# re: How to handle ""Maximum request length exceeded" exception
Requesting Gravatar...
<system.web>
<httpRuntime maxRequestLength="10000000" targetFramework="4.6" />
</system.web>
Left by Sumit on Feb 19, 2018 6:25 AM

Your comment:
 (will show your gravatar)


Copyright © Sglima | Powered by: GeeksWithBlogs.net