I’m a web developer

Icon

Passion for the web, asp.net, css och javascript

Check the web.config customErrors mode with code

If you have custom error pages in your project chances are that you’ve overridden the OnError event in a page or a base class for all your pages. In this case the customErrors settings in the web.config file is ignored. It’s pretty easy to check this settings manually.

 

private CustomErrorsMode CustomErrorsMode
{
    get
    {
        Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
        CustomErrorsSection section = (CustomErrorsSection)config.GetSection("system.web/customErrors");
        
        if (section != null)
        {
            return section.Mode;
        }
        else
        {
            return CustomErrorsMode.Off;
        }
    }
}

The CustomErrorsMode enum can have these values:

  • On
  • Off
  • RemoteOnly

Then you need to check whether you should redirect to your error page or not. If RemoteOnly is used you need to check the users IP address.

private bool ShowErrorPage
{
    get
    {
        CustomErrorsMode mode = CustomErrorsMode;
        switch (mode)
        {
            case CustomErrorsMode.Off:
                return false;
            case CustomErrorsMode.On:
                return true;
            case CustomErrorsMode.RemoteOnly:
                return Request.UserHostAddress != "127.0.0.1";
            default:
                return false;
        }
    }
}

Finally you check the ShowErrorPage property in your OnError event handler.

protected override void OnError(EventArgs e)
{
    if (ShowErrorPage)
    {
        Server.Transfer("~/ErrorPages/500.aspx");
    }
}
About these ads

Filed under: ASP.NET, web.config

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Pages

Archives

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: