1
votes

I'm trying to handle errors and show appropriate pages for 404, 403 and so on.

To do so, I've made an ErrorHandling filter and It works as expected. Namely, when I try to request

mysite.com/Home/random_non-existent_action

user is redirected to my custom 404 page and returned 404 as response code. Same applies for 401 and 403 as well.

I' ve achieved this with Error Handling filter and modification made on web.config as :

enter image description here

This is where the problem starts, our customer wants to customize IIS' error pages as well, for instance when I request

mysite.com/images,

IIS error page is being shown because directory browsing is disabled and because this is prevented at IIS-level, my error handling filter does not work.

To override IIS error pages, I'v added below code to web.config

enter image description here

Now with both customErrors and httpErrors are present in my web.config, when I request

mysite.com/Home/random_non-existent_action

my custom 404 page is shown (because It's by-passed by IIS and my error handling filter works). But when I request

mysite.com/content

or

mysite.com/images

I'm getting a blank response with 404 status code.

When I change responseMode to Redirect (instead of ExecuteURL), It works. But clearly I'm trying to navigate to my custom error pages with relative paths.

1-Why am I getting a blank response when I try to override IIS custom error pages?

2-Why does It work when I make responseMode="Redirect" but not responseMode="ExecuteURL"

Any help would be greatly appreciated as I've searched many similar stackoverflow topics with no success.

I've tried all combinations for existingResponse and responseMode values. Anything except responseMode="Redirect" makes it go blank with corresponding status code.

EDIT :

What I've done in the end is,

enter image description here

Write html content and redirect to my custom error pages with javascript.

1
Does the solution for this question help you at all? stackoverflow.com/questions/9997772/…Steve Ruble
Nope it does not. Directory browsing is already installedOzanYukruk

1 Answers

1
votes

In MVC 5, I had the same kind of problem. I resolve this handling these errors at Global.asax. Put below code in Global.asax.cs.

public void Application_Error(Object sender, EventArgs e)
    {
        Exception exception = Server.GetLastError();
        Server.ClearError();

        var routeData = new RouteData();
        routeData.Values.Add("area", "");
        routeData.Values.Add("controller", "SystemAdministration");

        if (exception.GetType() == typeof(HttpException))
        {
            var code = ((HttpException) exception).GetHttpCode();
            if (code == 404)
            {
                routeData.Values.Add("action", "NotFound");
            }
            else if (code == 403 || code == 401)
            {
                routeData.Values.Add("action", "NoAuthorization");
            }
            else
            {
                routeData.Values.Add("action", "Index");
                routeData.Values.Add("statusCode", code);
            }
        }
        else
        {
            routeData.Values.Add("action", "Index");
            routeData.Values.Add("statusCode", 500);
        }
        routeData.Values.Add("exception", exception);

        Response.TrySkipIisCustomErrors = true;
        IController controller = new ErrorController();
        controller.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
        Response.End();
    }

Hope this will works for you...