How to catch ConfigurationErrorsException for violating maxRequestLength?

后端 未结 5 1543
[愿得一人]
[愿得一人] 2020-12-15 10:26

I am limiting file size users can upload to the site from Web.config. As explained here, it should throw a ConfigurationErrorsException if size is not accepted. I tried to c

5条回答
  •  春和景丽
    2020-12-15 11:14

    You cant catch error in action method becouse exception comes earlier, but you can catch it here

    protected void Application_Error() {
         var lastError = Server.GetLastError();
         if(lastError !=null && lastError is HttpException && lastError.Message.Contains("exceed")) {
          Response.Redirect("~/errors/RequestLengthExceeded");
          }
        }   
    

    Actualy when file size exceeds limits HttpException error arise.

    There is also IIS limit on content - wich can't be catched in application. IIS 7 throws

    HTTP Error 404.13 - Not Found The request filtering module is configured to deny a request that exceeds the request content length.

    You can google it, there is a lot of information about this iis error.

提交回复
热议问题