14

I found another question posted on here which provided a solution to the first part of my question, i.e. how to generate a 404 page in ASP.NET MVC WITHOUT redirecting to another page (so the originally requested url remains in the address bar).

ASP.NET MVC - How to throw a 404 page similar to that on StackOverflow

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

        Response.Clear();
        var httpException = exception as HttpException;
        var routeData = new RouteData();
        routeData.Values.Add("controller", "Error");

        if (httpException != null)
        {
            routeData.Values.Add("action", httpException.GetHttpCode() == 404 ? "NotFound" : "Unknown");

            // clear the error, otherwise, we will always get the default error page.
            Server.ClearError();

            // call the controller with the route
            IController errorController = new ErrorController();
            errorController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));

        }
    }

The 404 side of things works fine for me if I don't use an IOCControllerFactory, but as soon as I do use a factory, the error code in global.asax isn't used and instead I get the following error:

The IControllerFactory 'MyNamespace.IOCControllerFactory' did not return a controller for the name 'blah'.

How do I get around this, without resorting to getting rid of my controllerfactory, and creating a parameterless constructor for each of my controllers?

Community
  • 1
  • 1
marcusstarnes
  • 6,393
  • 14
  • 65
  • 112
  • See [this post](http://stackoverflow.com/questions/619895/how-can-i-properly-handle-404-in-asp-net-mvc/7967759#7967759), which is an example of a custom 404 error page solution using ASP.Net MVC filter attributes. This solution also avoids the 302 / 200 messages to the browser. The browser gets a 404 response. – sky-dev Nov 01 '11 at 14:20

1 Answers1

14

ASP.NET can do this OOTB (starting from 3.5). On the <CustomErrors> element add: redirectMode=ResponseRewrite

Update

I posted the wrong code. It is IIS 7 which handles this.

<system.webServer>
    <httpErrors errorMode="Custom" existingResponse="Replace">
         <remove statusCode="404" />
         <error statusCode="404" path="/home/notfound" responseMode="ExecuteURL" />
     </httpErrors>
<system.webServer>
Mark
  • 473
  • 6
  • 7
  • This doesn't work under MVC though unfortunately, only web forms. I had found another article on this and tried it but no joy for MVC :( – marcusstarnes Aug 27 '11 at 09:22
  • The code I posted was from one of my own ASP.NET MVC projects. Works fine :) It's tested using IIS 7. What webserver did you test with? I'm not sure it will work with the Visual studio's built in dev. server. – Mark Aug 27 '11 at 21:13
  • I just tried this updated code as above (both with and without an additional customerrors section in the system.web section) but no success unfortunately. I'm actually getting a 403 forbidden when I hit an error, instead of my custom error page. When I added an additional customerrors section to the system.web section, it also gave 403 but mentioned SSL(?!), but checking my IIS7 configuration, I don't have SSL turned on for any of this web application. Confused! – marcusstarnes Aug 31 '11 at 10:05
  • 1
    That sounds strange. You can try to use the this little nuget, [NotFoundMvc](https://github.com/andrewdavey/NotFoundMvc). It contains various _tricks_ to catch 404 errors and display a friendly page. – Mark Aug 31 '11 at 11:41
  • 1
    if you have an error tag in customErrors it should be removed in order to this configuration work – Lelis718 Sep 06 '15 at 06:14