c# - Catching "Maximum request length exceeded" -
i'm writing upload function, , have problems catching "system.web.httpexception: maximum request length exceeded" files larger specified max size in httpruntime
in web.config (max size set 5120). i'm using simple <input>
file.
the problem exception thrown before upload button's click-event, , exception happens before code run. how catch , handle exception?
edit: exception thrown instantly, i'm pretty sure it's not timeout issue due slow connections.
there no easy way catch such exception unfortunately. either override onerror method @ page level or application_error in global.asax, check if max request failure and, if so, transfer error page.
protected override void onerror(eventargs e) ..... private void application_error(object sender, eventargs e) { if (globalhelper.ismaxrequestexceededexception(this.server.getlasterror())) { this.server.clearerror(); this.server.transfer("~/error/uploadtoolarge.aspx"); } }
it's hack code below works me
const int timedoutexceptioncode = -2147467259; public static bool ismaxrequestexceededexception(exception e) { // unhandled errors = caught @ global.ascx level // http exception = caught @ page level exception main; var unhandled = e httpunhandledexception; if (unhandled != null && unhandled.errorcode == timedoutexceptioncode) { main = unhandled.innerexception; } else { main = e; } var http = main httpexception; if (http != null && http.errorcode == timedoutexceptioncode) { // hack: no real method of identifying if error max request exceeded // treated timeout exception if (http.stacktrace.contains("getentirerawcontent")) { // max request has been exceeded return true; } } return false; }
Comments
Post a Comment