I want to be able to capture the exception that is thrown when a user requests a non-existent controller and re-direct it to a 404 page. How can I do this?
我希望能够捕获当用户请求不存在的控制器并将其重定向到404页面时引发的异常。我怎样才能做到这一点?
For example, the user requests http://www.nosite.com/paeges/1
(should be /pages/
). How do I make it so they get re-directed to the 404 rather than the exception screen?
例如,用户请求http://www.nosite.com/paeges/1(应为/ pages /)。如何制作它以便重新定向到404而不是异常屏幕?
3 个解决方案
#1
#2
16
Just use a route:
只需使用一条路线:
// We couldn't find a route to handle the request. Show the 404 page.
routes.MapRoute("Error", "{*url}",
new { controller = "Error", action = "404" }
);
Since this will be a global handler, put it all the way at the bottom under the Default route.
由于这将是一个全局处理程序,所以将它一直放在默认路由下的底部。
#1
#2
16
Just use a route:
只需使用一条路线:
// We couldn't find a route to handle the request. Show the 404 page.
routes.MapRoute("Error", "{*url}",
new { controller = "Error", action = "404" }
);
Since this will be a global handler, put it all the way at the bottom under the Default route.
由于这将是一个全局处理程序,所以将它一直放在默认路由下的底部。