Creating a failover route in MVC 5

258 views Asked by At

I would like it if any Url that is not found is sent to my "Home" controller. And I mean like total garbage urls passed in Like SomeDomain/ThisIsNotAController/Goober/adsfasdf1?spacerocket. A 404 error is just confusing to many people.

How do I map a route to say Home/Index if all others fail. I know you can do this in Rails. Thanks in advance. and yes I did search for this, but I guess I don't know what to ask for

TW

1

There are 1 answers

0
Leo On

You could use custom errors in the web.config file for this purpose by specifying an action method in the controller you wish...

public ActionResult NotFound(string aspxerrorpath)
{
    LogManager.Current.LogError(aspxerrorpath);
    Response.StatusCode = 404;
    return View();
}

Create the view as you normally would and then add the custom errors element in the web.config file...

<customErrors allowNestedErrors="true" mode="On">
   <error statusCode="404" redirect="~/Your_Controller/NotFound"/>
</customErrors>