Question

I have created custom views for an MVC app.

I let the errors bubble up to the Application_Error handler in my Globals.asax file and perform a redirect to an error controller which renders those views:

protected void Application_Error(object sender, EventArgs eventArgs)
{
    Exception exception = Server.GetLastError();
    Response.Clear();
    var httpException = exception as HttpException;

    Response.TrySkipIisCustomErrors = true;

    if (httpException != null)
    {
        switch (httpException.GetHttpCode())
        {
            case 404:
                Server.ClearError();
                Response.Redirect(UiConstants.PageNotFoundPage);
                break;
            case 500:
                if (httpException.Message.Equals(UiConstants.NoSessionStateExceptionMsg, StringComparison.Ordinal))
                {
                    Server.ClearError();
                    Response.Redirect(UiConstants.SessionNotAvailablePage);
                }
                else
                {
                    Server.ClearError();
                    Response.Redirect(UiConstants.ServerErrorPage);
                }
                break;
            default:
                Server.ClearError();
                Response.Redirect(UiConstants.ServerErrorPage);
                break;
        }
    }
    else
    {
        Server.ClearError();
        Response.Redirect(UiConstants.ServerErrorPage);
    }
}

In my development environment (VS Dev Web Server), this works as designed. When I deploy this to a test server, the stock IIS error web pages display, instead of my views.

The route path is correct i.e. servername/StaticContent/PageNotFound

Any ideas how to resolve this? Is there some IIS setting doing this?

Was it helpful?

Solution

I believe I have identified my problem. Running locally, the base address was just http://localhost:64133/ . So all of my redirects were working as they were using relative paths e.g. http://localhost:64133/staticcontent/pagenotfound

However, when I deployed to my dev server in our dev environment, the path of the base directory was slightly different because the IIS application was inside a folder called WebRequest, which was sitting inside of wwwroot (in C:\inetpub).

This meant that the base URL for the application was http://servername/WebRequest/ . And so my redirects were sending it to http://servername/staticcontent/pagenotfound, instead of http://servername/WebRequest/staticcontent/pagenotfound .

Sorry about that folks. That would have been very difficult for you guys to help, not knowing that environmental foible. I guess we can all learn from that one. Thanks again.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top