简体   繁体   中英

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.

您可以检查Global.aspx文件中的Request.TotalBytes属性,如果TotalBytes超出您的限制,则将您的请求重定向到错误页面。

Edit your web.config.

under <system.web>

Change the value as necessary

<httpRuntime maxRequestLength="4096" />

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM