1

I'm trying to error out more gracefully when a user attempts to upload a file that's too large for the server. However, when I try to capture the error in Global.asax.cs, it seems to skip right past my redirection code. Here's the core of what I have.

protected void Application_Error(object sender, EventArgs e)
{
   HttpContext.Current.ClearError();
   Response.Redirect("CustomErrors.aspx", false);
   Context.ApplicationInstance.CompleteRequest();
}

Everywhere I look the 'answer' seems to be to clear the error, but I've already done that. What else could possibly be going wrong. Any ideas?

Dalal
  • 1,058
  • 2
  • 15
  • 38

3 Answers3

1

I know this is an older thread but I found a way around this...

In the case below Google bot was passing bad URL characters and I wanted to just redirect to default.

After you trap the error Server.ClearError(); then you can redirect to any page on your site.

void Application_Error(object sender, EventArgs e)
{
    //... other code here....

    var Message = ex.Message.ToLower();
    var RootURL = Common.GetAppPathMasterPages() + "default.aspx";

    if (Message.Contains("does not exist") || Message.Contains("potentially dangerous request"))
    {
        Server.ClearError();
        HttpContext.Current.Response.Redirect(RootURL);
        return;
    }    

}

Jeroen K
  • 9,598
  • 5
  • 37
  • 38
Patrick
  • 1,545
  • 12
  • 20
0

I've just tried your code and when I raise an unhandled error in Page_Load() of my test page and then attempt to redirect with HttpContext.Current.ClearError() present in the Application_Error() method I receive a

Cannot Redirect after HTTP header have been sent error

Removing this ClearError() method, solves this problem.

If however, I raise the event within an event on the page (i.e. a button click), this works fine even with calling HttpContent.Current.ClearError().

One thing I did notice is you haven't got a '~/' in front of the 'CustomError.aspx', you may want to add this so that the page is found regardless of where the error is raised. (I think I you had multiple folders and an error happens, the context will be in the current folder).

A better way of adding a custom error page is just to modify the web.config with the necessary configuration, this will perform the redirect for you. You can still then log the error (via Log4Net or equivalent) in the Application_Error() method. You may find this question, helpful if you want to display the exception on your custom error page.

From this link

<configuration>
    ...

    <system.web>
        <customErrors mode="RemoteOnly"
                      defaultRedirect="~/ErrorPages/Oops.aspx" />

        ...
    </system.web>
</configuration>
Community
  • 1
  • 1
Damon
  • 2,987
  • 7
  • 21
  • 28
  • "the header of the page have already been sent to the client by the time the Page_Load() method has been called." Erm? No they haven't. They *have* been raised by the time the `Application_Error` event has fired, though. – Ant P Dec 14 '13 at 10:19
  • Thanks for the correction @AntP, I'll modify my answer accordingly. – Damon Dec 14 '13 at 10:23
  • It's worth also noting that `Page_Load` actually happens *before* other event handlers such as those for button clicks (your answer seems to suggest it's the other way around), so it's perfectly reasonable to redirect within a `Page_Load` method. – Ant P Dec 14 '13 at 10:26
  • @AntP Agreed, after some more testing realised it was the ClearError() method that was causing the problem with the exception in Page_Load(). I don't quite understand why that happens in Page_Load() and not in the button click event though. I don't really see a need for ClearError() before the redirect. – Damon Dec 14 '13 at 10:45
  • The 'CustomError.aspx' was just an example, The best way to replicate my problem is to set up a FileUpload object and try to upload a file that's too big for the server. Everywhere I look the general answer resembles what is written here: http://www.developer.com/db/article.php/10920_3426051_2 It must be outdated, because it's not working for me. – Dalal Dec 14 '13 at 20:34
  • @Dalal Ahh, I think that may be a slightly different question. I think you could use the Ajax File Upload Control? Not sure if that's an option? Check out this question: http://stackoverflow.com/questions/17865119/how-to-check-file-size-of-each-file-before-uploading-multiple-files-in-ajaxtoolk – Damon Dec 15 '13 at 19:46
0

An alternative is to ensure the following is set.

Response.BufferOutput = true;

This way a response is not sent (including the headers) until the the responce is ready to be sent.

See here.

Server cannot set status after HTTP headers have been sent IIS7.5

Community
  • 1
  • 1
CountZero
  • 5,431
  • 3
  • 41
  • 56
  • I tried that. It still does not redirect. Please see the following link for a description of the problem I'm having and the answer which doesn't work: http://www.developer.com/db/article.php/10920_3426051_2 – Dalal Dec 14 '13 at 20:35