4

This is my first post here, and I have searched for a resolution to this issue here and at many other forums on the web, without success.

I am attempting to create a custom error for directory access denied errors 403.14 for cases where say if someone tries to load the "_assests" directory on a web site. I know I can do a work around by adding a default.aspx page to each directory that I want this to happen with, but was wondering if there is a site wide solution similar to the tag in the web.config file

<configuration>
<system.web>
    <customErrors defaultRedirect="/Errors/GenericError.aspx" mode="RemoteOnly">
      <error statusCode="401"
         redirect="/Errors/401.aspx"/>
      <error statusCode="403"
         redirect="/Errors/403.aspx"/>
      <error statusCode="404"
         redirect="/Errors/404.aspx"/>
    <!-- 
      <error statusCode="403.14"
         redirect="/"/>
    -->
    </customErrors>
</system.web>
</configuration>

I get the error when coding the web.config that I am unable to use a statusCode with a decimal in it because it's not a datatype Int

I have an IIS 7 on Server 2008.

Any ideas?

Anders Lindén
  • 6,839
  • 11
  • 56
  • 109
mappingman
  • 123
  • 2
  • 13

2 Answers2

2

Apologies ahead of time if this sounds rather confusing. Happy to clarify.

Added the following to the web.config and it seems to work so far. Don't know why, but if I don't explicitly tell 403 errors to redirect to a custom 403.aspx page, instead of the GenericError.aspx page, I get a 500 error. However, if I redirect 404 errors to my custom 404.aspx page, the GenericError.aspx code is written in place, not as expected, and it seems you are never redirected to the actual 404.aspx page (see commented portion of web.config). Bizarre.

CODE:

web.config file:

<system.webServer>
    <httpErrors existingResponse="Replace" errorMode="Custom">
        <remove statusCode="403"/>
        <remove statusCode="404"/>
        <error statusCode="403" path="/Errors/403.aspx" responseMode="Redirect"  />
<!-- <error statusCode="403" path="/Errors/GenericError.aspx" responseMode="Redirect"  /> -->
<!-- <error statusCode="404" path="/Errors/GenericError.aspx" responseMode="Redirect"  /> -->
<error statusCode="404" path="/Errors/404.aspx" responseMode="Redirect"  />
    </httpErrors>
</system.webServer>

GenericError.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{

    var ex = HttpContext.Current.Server.GetLastError();

    if (ex is HttpException)
    {
        var nex = ex as HttpException;
        //Label in the Main code displays the Error Code from the Error String
        this.customErrorMessageCode.Text += "Error Code" + " " + nex.GetHttpCode().ToString();
        //Label in the Main code displays the Error Message from the Error String
        this.customErrorMessageLabel.Text += ex.Message.ToString();
        //DIV ID in the Main code displays the entire error message
        this.customErrorMessage.Visible = true;
        switch (nex.GetHttpCode())
        {
            case 404:
                this.customErrorMessageCode.Text += " Page Not Found";
                this.customErrorMessageImage.Visible = true;
                // do somehting cool
                break;
            case 403:
                this.customErrorMessageCode.Text += " Forbidden Access";
                this.customErrorMessageImage.Visible = true;
                // do somehting cool
                break;
            case 500:
                this.customErrorMessageCode.Text += " Internal Error";
                this.customErrorMessageImage.Visible = true;
                // do somehting cool
                break;
            default:
                break;
        }
    }
    else {
        this.customErrorMessageLabel.Text += ex.Message + ex.GetType().ToString();
    }

}

SOURCE:

CustomError in web.config

Community
  • 1
  • 1
mappingman
  • 123
  • 2
  • 13
1

Perhaps you could use a redirection approach that is based on the actual error code using Server.GetLastError().

In the Global.asax you will have something like this:

protected void Application_Error(object sender, EventArgs e)
{
    if (Context.IsCustomErrorEnabled) {
        ShowCustomErrorPage(Server.GetLastError());
    }
}

ShowCustomErrorPage would then have a switch statement that reads the HTTP code and redirects to the correct error page.

The is more from the source link, but it might be too MVC specific. As you didn't mention you use MVC, I didn't want to assume and blindly copy-paste.

I am not too familiar with MVC, but the principles here look like they could be adjusted to suit your scenario.

Source: http://www.digitallycreated.net/Blog/57/getting-the-correct-http-status-codes-out-of-asp.net-custom-error-pages

Edit

Found a couple of StackOverflow posts that could help too:

Custom Error Handling in web.config / Global.asax not handling non-existant directory

Custom error handling Asp.Net

Community
  • 1
  • 1
Ash Clarke
  • 4,807
  • 1
  • 37
  • 48