0

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.

Arti
  • 2,993
  • 11
  • 68
  • 121
  • 1
    I am not on my pc but there is two ways you can do that. One is through the client side js you can stop people posting large files that way. The second way is to when you receive the request to check the request length if its larger then required you bail out and return an error to the user. When i get to work i will put a answer down for you. – dmportella Jun 24 '14 at 05:59
  • @dmportella thanks for the reply. I would like to know the way to handle the error (ie. how to return an error to the user) – Arti Jun 24 '14 at 10:10
  • Admins, close this "which is the best way", please, because: As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. – Alan Turing Jun 24 '14 at 10:31
  • also, 100% it is a duplicate & fake – Alan Turing Jun 24 '14 at 10:32
  • 1
    @dmportella Any suggestion for handling this? I did validations on the clients side to restrict uploading files, but would also like to know how it can be handled at server side – Arti Jul 07 '14 at 11:01
  • If you ate blocking the file from being uploaded you wont be able to find out. Someone could bypass your js and try to upload directly to the front end so you need to stop the request by checking the request length at the back end. – dmportella Jul 07 '14 at 11:13
  • Yes I know the risk, but could not get it working. Any suggestion from your side @dmportella? – Arti Jul 07 '14 at 11:47
  • I will update you when i get home ;) – dmportella Jul 07 '14 at 16:39

3 Answers3

2

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

Ishtiaq
  • 980
  • 2
  • 6
  • 21
  • OK. I will try this. But could you please explain what file the error page should have. Should it be an image or a html page or an aspx page? – Arti Jun 24 '14 at 10:32
  • Error page should be some aspx page to which you can redirect your request. – Ishtiaq Jun 24 '14 at 10:35
  • Ok. Thanks. I will try your answer and will let you know – Arti Jun 24 '14 at 10:38
  • Nope. The solution you provided isn't working. It does not redirect to the other page but remains on the same page with message "This web page is not available" – Arti Jun 30 '14 at 17:23
  • It seems you are not giving the correct path of error page. – Ishtiaq Jul 01 '14 at 06:33
  • Error page is in the root directory, I am redirecting it to the error page like this: Response.Redirect("Error.aspx"); – Arti Jul 01 '14 at 06:42
  • I tried debugging the code and found that redirection line is hit but the page is not getting redirected, also the url of the page remains same i.e the page on which error occurs – Arti Jul 01 '14 at 06:43
  • Try this if you have error page at root. Response.Redirect(String.Format("http://{0}/Error.aspx", Request.ServerVariables["SERVER_NAME"])); – Ishtiaq Jul 01 '14 at 06:46
  • what will be "SERVER_NAME"? – Arti Jul 01 '14 at 06:49
  • SERVER_NAME will give you the server's host name. – Ishtiaq Jul 01 '14 at 07:06
  • It still doesn't work with Response.Redirect(String.Format("http://{0}/Error.aspx", Request.ServerVariables["SERVER_NAME"])); – Arti Jul 01 '14 at 15:14
  • Its the same "This web page is not available" – Arti Jul 02 '14 at 09:20
0

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;
}
d.popov
  • 4,175
  • 1
  • 36
  • 47
-1

Edit your web.config.

under <system.web>

Change the value as necessary

<httpRuntime maxRequestLength="4096" />
CurseStacker
  • 1,079
  • 8
  • 19
  • It is not supposed to handle an error; You should change it for you to be able to handle the error. see more. http://www.ezineasp.net/post/ASP-Net-FileUpload-Size-Limit-Example.aspx – CurseStacker Jun 20 '14 at 11:05
  • 1
    He doesnt want to prevent the error he wants to handle the error gracefully. – dmportella Jun 24 '14 at 05:56
  • I want to keep max upload size the same, and handle the exception if in case user tries to upload file greater than the specified size. – Arti Jun 24 '14 at 10:08