I'm trying to build my tutorial project with routing. My main objective is to build two routes which won't generate 404 error in any case. By this I mean that if the path is wrong I want routing to use /Home/Index path. I have two following routes -
routes.MapRoute("Default", "{controller}/{action}",
new {controller = "Home", action = "Index"}
);
routes.MapRoute("Second", "{*catchall}",
new {controller = "Home", action = "Index", id = UrlParameter.Optional}
);
It works fine when I use nonexistent path which doesn't matches the first route, like this -
But if it does, then I have the following -
or
I understand the reason why it happens. However at the present moment, I only managed to find 'some sort' of a solution. Adding the following code to web.config file -
<customErrors mode="On">
<error statusCode="404" redirect="~/Home/Index"/>
</customErrors>
However, I don't think that it is the best way to solve this problem. Because, as far as I can understand, it simply catches all errors and redirects it to the correct path, without actually using routing. So I don't think I need this global handling.
So could somebody please give me a hint or provide me with a good solution to my problem. Thank you.