0

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?

onefootswill
  • 3,707
  • 6
  • 47
  • 101
  • 1
    http://stackoverflow.com/questions/8732048/mvc3-custom-error-pages-work-in-dev-not-on-server -- Check this, might help!!! – PSL Apr 04 '13 at 02:12
  • Turn off custom errors in your deployment web.config http://support.microsoft.com/kb/815166 – Dave Alperovich Apr 04 '13 at 02:56
  • Should it be like this `\servername\StaticContent\PageNotFound` ? –  Apr 04 '13 at 05:20
  • @PSCoder Thanks for that, but my code already includes the statement Response.TrySkipIisCustomErrors = true; – onefootswill Apr 04 '13 at 05:49
  • @DaveA custom errors in web.config does not come into it. I'm working outside that mechanism by redirecting to my own routes. But it is handy to see stack-traces during development anyway. And I would not put a site into production with CustomErrors turned off, as that would be a security vulnerability. – onefootswill Apr 04 '13 at 06:02
  • @PankajGarg Internet URLs contain forward-slashes. Files system paths (for Windows) use back-slashes. Routes in ASP.NET MVC do not use back-slashes. Thanks. – onefootswill Apr 04 '13 at 06:05
  • 1
    if you are using iis7> then i would check that detailed errors are turned on, otherwise i think it will just push in its minimalistic ones – Slicksim Apr 05 '13 at 13:49

1 Answers1

0

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.

onefootswill
  • 3,707
  • 6
  • 47
  • 101