locked
custom error HttpModule gives Cannot redirect after HTTP headers have been sent error RRS feed

  • Question

  • User-884647787 posted

    Hi,

    I am using SharePoint 2010 and one of my asp.net user controls gives an exception. I also have a custom Http Error module. The error module is working fine on other errors When I debug I get into my error module. When I try to redirect I get the exception "Cannot redirect after HTTP headers have been sent error" How can I change this module so it works for this situation? See my code from my HttpModule

    This line: HttpContext.Current.Response.Redirect("/Pages/ErrorPage.aspx"); Gives the exception. I am not doing any other redirects.

    using System;
    using System.Text;
    using System.Web;
    using Common.Constants;
    using Common.ExceptionHandling;
    namespace ErrorHandlingHttpModule
    {
        public class ErrorHandlingHttpModule : IHttpModule
        {
            private HttpApplication app;
            public ErrorHandlingHttpModule()
            {
            }
            public void Dispose()
            {
            }
            public void Init(HttpApplication HTTPContext)
            {
                app = HTTPContext;
                //register the handler for errors
                HTTPContext.Error += new EventHandler(HTTPContextErrorHandler);
            }
            void HTTPContextErrorHandler(object sender, EventArgs e)
            {
                try
                {
                    HttpResponse response = app.Response;
                    // find all errors and log them, if neccesary we can pass these
                    // as url parameter to the error page
                    Exception[] ExceptionList = HttpContext.Current.AllErrors;
                    if (ExceptionList != null && ExceptionList.Length > 0)
                    {
                        if (ExceptionList[0].GetType().Equals(typeof(HttpUnhandledException)) && (ExceptionList[0].InnerException != null))
                        {
                            HttpContext.Current.Session["Exceptions"] = ExceptionList[0].InnerException;
                        }
                        else if (ExceptionList[0].InnerException != null)
                        {
                            HttpContext.Current.Session["Exceptions"] = ExceptionList[0].InnerException;
                        }
                    }
                    foreach (Exception ex in ExceptionList)
                    {
                        ExceptionHandler.LogException(ex);
                    }
    		HttpContext.Current.Server.ClearError();
    		HttpContext.Current.Response.Clear()
    
                    HttpContext.Current.Response.Redirect("/Pages/ErrorPage.aspx");
                }
                catch (HttpException ex)
                {
                    ExceptionHandler.LogException(ex);
                }
            }
        }
    }



    Thursday, October 10, 2013 8:47 AM

All replies