Arti
Arti

Reputation: 3071

Handling maximum request length exceeded

I would like to know, which is the best way to handle "maximum request length exceeded" error in my application. I have asp.net application in which the user is allowed to upload file(pdf or image). I would like to handle the error. I did some research and found that it can be handled in global.asax, but I am not sure about what has to be done. As far as I understood, I will have to handle it in global.asax file and redirect it to custom error page. Could anyone please suggest what and how the custom error page should be?

Should it be a HTML page or a jpg file or aspx file? And what should be its content? Can I redirect it to the same page on which error occurred? If yes then it would be easier for me to just display an error message on the same page.

Update I did client side validations to restrict users from uploading large file. But still would like to how can the issue be fixed at server side.

Upvotes: 0

Views: 2342

Answers (3)

d.popov
d.popov

Reputation: 4255

The error can be handled in Global.asax: Application_Error() as so:

var ex = Server.GetLastError();
var httpException = ex as HttpException ?? ex.InnerException as HttpException;
if (httpException.WebEventCode == WebEventCodes.RuntimeErrorPostTooLarge)
{
  Response.Clear();
  Server.ClearError();
  //Do whatever to handle
  return;
}

Upvotes: 0

Ishtiaq
Ishtiaq

Reputation: 1058

You can check for Request.TotalBytes Property in Global.aspx file and redirect your request to the error page if TotalBytes exceeded youre limit.

Upvotes: 2

CurseStacker
CurseStacker

Reputation: 1079

Edit your web.config.

under <system.web>

Change the value as necessary

<httpRuntime maxRequestLength="4096" />

Upvotes: -1

Related Questions