正确的方式来处理ASP.NET MVC系统错误 [英] Correct way to handle ASP.NET MVC system errors

查看:71
本文介绍了正确的方式来处理ASP.NET MVC系统错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是处理在ASP.NET MVC系统错误的最好方法?我看过对 DimeCast.net 视频中的家伙用global.ascx文件写的Application_Error 的方法来处理错误。但目前,我管我的的web.config 文件。

Which is the best way to handle a system error in ASP.NET MVC? I've watched a video on DimeCast.net in which the guy used the global.ascx file to write a method Application_Error to handle the errors. But currently, I'm handling my errors inside the web.config file.

有人能指出我对如何妥善处理错误的方向是正确的?这可能是:

Could someone point me in the right direction on how to properly handle errors? This could be:


  1. 语法错误

  2. 例外

  3. 404和其他网页/文件中未发现的错误

该应用程序是相当大的。

The application is fairly large.

推荐答案

很可能你将需要相结合的办法。有些错误可以直接在你的模型在控制器中进行处理,其他人,和其他一些人可能需要在应用层面来处理所有的方式(通过web.config中或通过global.ascx。)

Very likely you are going to need a combination of approaches. Some errors can be handled directly in your models, others in your controllers, and some others might need to be handled all the way to the application level (either via web.config or via the global.ascx.)

我preFER以处理global.ascx而不是通过在web.config中重定向的错误,因为在global.ascx我对引起错误的请求,在那里,如果我做通过重定向的更多信息在web.config中设置的一些信息是由我的错误页面收到请求时遗失的。

I prefer to handle the errors in the global.ascx rather than via redirection in web.config because in the global.ascx I have more information about the request that caused the error where as if I do a redirect via the web.config settings some of that information is lost by the time my error page receives the request.

几个星期前,我只注意到404错误最好不要通过web.config中处理,因为这(设计)导致HTTP重定向code 302,而不是一个真正的HTTP 404 code指示客户端的资源不存在。我有我的博客后,我详细讨论这个 HTTP: //hectorcorrea.com/Blog/Returning-HTTP-404-in-ASP.NET-MVC

A few weeks ago I just noticed that 404 errors are better not handled via the web.config because that (by design) causes an HTTP redirect code 302 rather than a true HTTP 404 code that indicates the client that the resource does not exist. I have a post on my blog where I cover this in detail http://hectorcorrea.com/Blog/Returning-HTTP-404-in-ASP.NET-MVC

使用ELMAH作为布鲁克建议也是一个非常不错的主意。

Using ELMAH as Brook suggested is also a very good idea.

这篇关于正确的方式来处理ASP.NET MVC系统错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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