如何将用户重定向到ASP.NET MVC中的自定义404页面而不是抛出异常?

时间:2022-10-07 08:01:39

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


6  

Take a look at this page for routing your 404-errors to a specified page.

请查看此页面,将404错误路由到指定页面。

#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.

由于这将是一个全局处理程序,所以将它一直放在默认路由下的底部。

#3


1  

Found this on the same site - Strategies for Resource based 404s

在同一网站上找到了这个 - 基于资源的404s策略

#1


6  

Take a look at this page for routing your 404-errors to a specified page.

请查看此页面,将404错误路由到指定页面。

#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.

由于这将是一个全局处理程序,所以将它一直放在默认路由下的底部。

#3


1  

Found this on the same site - Strategies for Resource based 404s

在同一网站上找到了这个 - 基于资源的404s策略