Search code examples
c#asp.nethttphttpexception

Throwing an HttpException always sends back HTTP 500 error?


I'm trying to throw an HTTP 403 error code back at the client. I've read that HttpException is the cleanest way to accomplish this, but it's not working for me. I throw the exception from within a page like this:

throw new HttpException(403,"You must be logged in to access this resource.");

However, this will only give a standard ASP.Net stack trace(with 500 error) when CustomErrors is off. If CustomErrors is on, then this will not redirect to the page I have setup to be displayed when a 403 error occurs. Should I forget about HttpException and instead set all the HTTP codes myself? How do I fix this?

The custom errors part of my Web.Config is this:

<customErrors mode="On" defaultRedirect="GenericErrorPage.html">
      <error statusCode="403" redirect="Forbidden.html" />
</customErrors>

Instead of getting Forbidden.html, I'll get GenericErrorPage.html


Solution

  • I've actually ended up making my own nifty little class to fix this problem. It doesn't handle everything and I'm not sure it plays nice with MVC, but it works for my use. Basically, instead of relying on ASP.Net to output the correct error page and error code, it will clear the error and then do a server-side transfer and display the appropriate Web.Config error page. It also recognizes the customErrors mode and reacts accordingly.

    public static class CustomErrorsFixer
    {
        static public void HandleErrors(HttpContext Context)
        {
            if(RunIt(Context)==false){
                return;
            }
            HttpException ex = Context.Error as HttpException;
            if(ex==null){
                try{
                    ex=Context.Error.InnerException as HttpException;
                }catch{
                    ex=null;
                }
            }
    
            if (ex != null){
                Context.Response.StatusCode = ex.GetHttpCode();
            }else{
                Context.Response.StatusCode = 500;
            }
            Context.ClearError();
    
            Context.Server.Transfer(GetCustomError(Context.Response.StatusCode.ToString()));
            HttpContext.Current.Response.End();
        }
        static protected string GetCustomError(string code)
        {
            CustomErrorsSection section = ConfigurationManager.GetSection("system.web/customErrors") as CustomErrorsSection;
    
            if (section != null)
            {
                CustomError page = section.Errors[code];
    
                if (page != null)
                {
                    return page.Redirect;
                }
            }
            return section.DefaultRedirect;
        }
        static protected bool RunIt(HttpContext context){
            CustomErrorsSection section = ConfigurationManager.GetSection("system.web/customErrors") as CustomErrorsSection;
            switch(section.Mode){
                case CustomErrorsMode.Off:
                    return false;
                case CustomErrorsMode.On:
                    return true;
                case CustomErrorsMode.RemoteOnly:
                    return !(context.Request.UserHostAddress=="127.0.0.1");
                default:
                    return true;
            }
        }
    
    }
    

    And then to activate it, just add a small thing to Global.asax

        protected virtual void Application_Error (Object sender, EventArgs e)
        {
            CustomErrorsFixer.HandleErrors(Context);
        }