Question

I want to trap 404 page not founds to go to a nice little sexy page I've created.
Let's start at step 1 first.

1)---------

I have an Error.chstml in my Shared views folder.

2)---------

I have added:

<customErrors mode="On" />

to my Web.Config (in the root directory) inside system.web

3)----------

I have added:

FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);

to my Global.asax ApplicationStart method and added a FilterConfig.cs file to my App Start folder with the following code:

public class FilterConfig
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
    }
}

4)------------

I then tried to add this to the customErrors XML:

<customErrors mode="On" defaultRedirect="/Error" />

5)--------------

I had tried to create my own ErrorController and used my routes to redirect to it... but they never get fired:

        routes.MapRoute(
            "404-PageNotFound",
            "{*url}",
             new { controller = "Error", action = "Error404" }
        );

...

public class ErrorController : Controller
{
    [AcceptVerbs(HttpVerbs.Get)]
    public ViewResult Error404()
    {
        return View();
    }
}

This still returns the ugly smelly web 1.0 error page :) : HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.


What am I doing wrong???? Something that I thought would take like 20 seconds has ended up taking 2 hours lol

Was it helpful?

Solution

With your current routes it will fail to redirect to that Error404 action method. Start by removing the 404-PageNotFound route. Then rename Error404 method to Index. Don't forget to rename the view too.

public class ErrorController : Controller
{
    public ViewResult Index()
    {
        return View();
    }
}

Now when user is redirected to http://example.org/Error like you specified web.config, this action method will be called.

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