一劳永逸什么是最好的路由方法来处理在MVC的错误,异常和404的 [英] Once and for all what is the best routing approach to handle errors, exceptions and 404's in MVC

查看:432
本文介绍了一劳永逸什么是最好的路由方法来处理在MVC的错误,异常和404的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有这么多的文章,并在尝试处理404的和异常优雅网页。

There are many articles on SO and the web in attempts to handle 404's and exceptions gracefully.

从我读过的最好的建议似乎是对404的像这样的路线:

From what I have read the best advice seems to be have a route for 404's like so:

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

那么对于其他错误已经在的HandleError 属性附加伤害的控制器,并且已在的customErrors web.config中开启如此这般的error.cshtml页面。

Then for other errors have the HandleError atribute on the Controller and have CustomErrors turned on in web.config so it goes to the error.cshtml page.

不过,我已阅读,如果你未在HTTP code设置为500异常的HandleError将无法工作。

However I have read that if you get a exception that does not set the HTTP code to 500 the HandleError will not work.

我们终于可以生产出404处理的/例外/ ASP.Net的错误,我们可以申请这对我们所有项目的答案/最佳做法?

Can we finally produce an answer/best practice that handles 404's/Exceptions/ASP.Net errors where we can apply to this to all our projects?

感谢

推荐答案

我用一个简单的错误处理设置。尼斯和简单。更多信息可在<一找到href=\"http://erictopia.com/2010/04/building-a-mvc2-template-part-7-custom-web-errors-and-adding-support-for-elmah/\" rel=\"nofollow\">http://erictopia.com/2010/04/building-a-mvc2-template-part-7-custom-web-errors-and-adding-support-for-elmah/

I use a simple error handling setup. Nice and simple. More info can be found at http://erictopia.com/2010/04/building-a-mvc2-template-part-7-custom-web-errors-and-adding-support-for-elmah/

安装ELMAH,并让它处理所有的错误。

Install ELMAH and have it handle all the errors.

接下来创建一个错误的控制器。添加一个catch像这样所有路线:

Next create an Error controller. Add a catch all route like this:

routes.MapRoute(
    "ErrorHandler", // Route name
    "{*path}",      // URL
    new { controller = "Error", action = "Index" }
);

然后在web.config中添加此部分:

Then in web.config add this section:

<customErrors mode="RemoteOnly" defaultRedirect="/Error/Index">
        <error statusCode="403" redirect="/Error/NoAccess" />
        <error statusCode="404" redirect="/Error/NotFound" />
</customErrors>

这篇关于一劳永逸什么是最好的路由方法来处理在MVC的错误,异常和404的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆