2

I have an MVC3 website and I'm trying to get the 404 error pages to show correctly. I've got it all set up that it

  • does not redirect the URL,
  • returns an actual 404 error status in the header,
  • logs the URL,
  • is a custom page I've created,
  • and is an actual action/controller page instead of a .aspx/.html page.

Debugging in Visual Studio, it works perfectly. When I post it to my test or production environment, it does not work and returns a generic file not found with the 404 status in the header.

The error page isn't the default 404 error ( this nor this ) but seems to be global custom on the server itself (the server hosts several sites, all 404s show the same simple line page). The error page on the server actually shows a 500 error page. Also, I've set Response.TrySkipIisCustomErrors = true to try to bypass it, but that doesn't work.

On the production and test servers, it is being logged correctly and if I have it not return the 404 status code, it displays my error page. So with that, I'm pretty sure the action/view are working, but it's somehow with the server. It's running ASP.NET 4 and IIS 7.

Any suggestions of what/where to investigate to fix this?

.

Resources of code I've based my solution thus far on
How can I properly handle 404 in ASP.NET MVC?

This is the base for what I'm doing by capturing the exception in Application_Error; I'm still a little wary of it because it could easily lead to a loop, so I've try/catched everything to protect against that (error happens within catching another error)

Code in global.asax

    protected void Application_Error(object sender, EventArgs e)
    {
            try
        {
            Exception exception = Server.GetLastError();

            Response.Clear();

            HttpException httpException = exception as HttpException;

            RouteData routeData = new RouteData();
            routeData.Values.Add("controller", "Error");

            if (httpException == null)
            {
                routeData.Values.Add("action", "NotFound");
            }
            else
            {
                switch (httpException.GetHttpCode())
                {
                    case 404:
                        // Page not found.
                        routeData.Values.Add("action", "NotFound");
                        break;
                    case 500:
                        // Server error.
                        routeData.Values.Add("action", "ServerError");
                        break;


                    default:
                        routeData.Values.Add("action", "General");
                        break;
                }
            }

            routeData.Values.Add("error", exception);

            Server.ClearError();

            Response.TrySkipIisCustomErrors = true;

            IController errorController = new ErrorController();
            errorController.Execute(new RequestContext(
                 new HttpContextWrapper(Context), routeData));
        }
        catch
        {
            //
        }
    }

Code in Error Controller

 public ActionResult NotFound()
    {
        try
        {

            Response.StatusCode = (int)HttpStatusCode.NotFound;
            Response.TrySkipIisCustomErrors = true;
        }
        catch
        {}
        return View();
    }
Community
  • 1
  • 1
Helba
  • 63
  • 5

2 Answers2

0

I had the same issue. The web.config file should be modified.

  <system.webServer>    
     <httpErrors errorMode="Custom" existingResponse="Replace">
        <remove statusCode="404" />
        <error statusCode="404" responseMode="ExecuteURL" path="/Errors/404" />
     </httpErrors>  
  </system.webServer>

Replace /Errors/404 with a path to your custom error page.

Andrei Schneider
  • 3,618
  • 1
  • 33
  • 41
  • I already have that set, but it isn't working. On local debug, if I get rid of my global application_error function, it doesn't do anything and shows the default [YSOD 404 error](http://static.colinmackay.co.uk/images/mvc/2011-05-13-Step-4-Resource-not-found-ysod.png) and on the server it shows the error that's been showing up all the time on the server for 404. I believe it's a custom 404 error for the actual server. – Helba Jul 13 '12 at 14:23
  • Just as a side note: Make sure the path to your 404 is right. I ran into an issue where I had my path screwed up and it defaults right back to the other 404 (My 404 was throwing 404's haha), so I thought this solution didn't work. Just double check it. – Saedeas Jul 13 '12 at 14:31
  • I already double checked that because knowing me, I'd type it wrong. But it's the correct path (copy/pasted and works locally, but doesn't work in test environment) I might see if I can contact the system admin to look into if it's enabled though [like this site shows](http://tedgustaf.com/en/blog/2011/5/custom-404-and-error-pages-for-asp-net-and-static-files/) – Helba Jul 13 '12 at 14:38
0

Have you specified the TrySkipIisCustomErrors as true? Check here.

VJAI
  • 32,167
  • 23
  • 102
  • 164
  • I have that set to true in the Application_error function and it doesn't do anything. I also set it in the controller when it loads the page just in case the setting didn't carry over, but that didn't work either. I've updated my question to reflect that and also that I realized the generic 404 error showing isn't asp.net's default. It looks to be a custom one for the server, showing up for all the sites hosted on the server. – Helba Jul 13 '12 at 14:18
  • Sorry it took a while, but I did just post my code. Also, I realized (and changed even though I'm not sure if I should have opened a new question) that the error thrown by the server( for what should be 404) actually has a 500 status code. – Helba Jul 17 '12 at 22:00