在ASP.NET MVC是什么在我看来,最能展现未处理的异常? [英] In ASP.NET MVC what is the best show unhandled exceptions in my view?
问题描述
我在我的web.config以下内容:
I have the following in my web.config:
<customErrors mode="On" defaultRedirect="Error">
<error statusCode="404" redirect="Error/NotFound" />
</customErrors>
我有一个
[HandleError]
在我HomeController类的顶部。为了测试,我创建并简单地抛出一个异常动作。 。并重定向到我的
at the top of my HomeController class. To test, I create and action that simply throws an exception . . and it redirects to my
ErrorController/Index
方法,但是当它到达我的看法,其结合HandleErrorInfo我的模型为空,所以我莫名其妙地失去了参考错误。
method but when it gets to my view which binds to HandleErrorInfo my model is null so I somehow have lost the reference to the error.
我相信它是与在重定向错误迷路,所以我想看看,如果我失去了一些东西,如果任何人有意见,我可以有一个观点,显示堆栈跟踪和错误信息。
I am sure it has something to do with the Error getting lost in the redirect so I wanted to see if i was missing something and if anyone had suggestions where I can have a view that shows the Stacktrace and error message.
推荐答案
我可以看到误解。你要做的 MVC
的事情,重定向
来一个控制器动作。
I can see the misconception. You want to do the MVC
thing and redirect
to a controller action.
但的defaultRedirect
本身就是一个 Web窗体
约定,因此限制了。您重定向到另一个控制器的那一刻,你会失去你的的HttpContext
,并因此而丧失你的 HandleErrorInfo
对象
But defaultRedirect
is itself a Web Form
convention and thereby limited. The moment you redirect to another controller, you will lose your HttpContext
, and thereby lose your HandleErrorInfo
Object
您 [的HandleError]
属性需要查看
指挥它的错误消息。通过上面的例子去,我假设你有你的 ErrorController
,并在它的查看/错误
文件夹中有一个首页
查看。如果你想你的过滤器上下文到 HandleErrorInfo
对象发送到该视图,
Your [HandleError]
Attribute requires a View
to direct it's error message to. Going by your example above, I assume that you have a Views/Error
Folder for your ErrorController
, and in it you have an Index
View. If you want to your Filter Context to send a HandleErrorInfo
object to that view,
试试这个语法:
[HandleError(View="~/Views/Error/Index")]
Public class HomeController : Controller
但是,我们记录?!?!?
我怀疑你的意图 更多的不仅仅是显示错误堆栈给用户。事实上,我怀疑你有没有这样的打算都没有。我怀疑你的真正目的是记录自己的错误(可能为DB),并显示一些平淡无奇的消息,你的用户是什么。
I suspect your intention is more than just displaying error stack to users. In fact, I suspect you have no such intention at all. I suspect what your real aim is to log your error (probably to db) and to display some bland message to your user.
我到目前为止已经解释了什么是最好的[办法]显示未处理的在我看来异常。在 [的HandleError]
属性是好了点。
What I've explained so far was "what is best [way to] show unhandled exceptions in my view". The [HandleError]
attribute is good for that.
但是,当你要移动到下一个步骤(记录错误),你有几个选择:
But when you want to move to the next step (logging the error) you have a few options:
1)重写你的基地控制器上异常的方法;创建你自己的控制器
从MVC控制器类继承,但覆盖在例外情况的方法。这种方法可以在[的HandleError]属性一起使用
1) Override your base controller's On Exception method; create your own Controller
inheriting from the MVC Controller class but override the On Exception Method. This approach can be used in conjunction with [HandleError] attribute
2)创建一个自定义异常处理程序创建自己的异常处理程序的记录错误。然后,您的异常处理程序可以调用选择的视图或可以结合 [的HandleError(订单= 2)]
,因为对应滤网属性可以取一个订单参数将precedence工作
2) Create a custom exception handler Create your own Exception Handler that logs the error. Your exception handler can then call a View of choice or can work in conjunction with [HandleError(order=2)]
since filter attributes can take an order argument applying precedence.
尼廷张以芳问一个错误观点是什么样子。在
Nitin Sawant asks what an error view would look like. The
@model System.Web.Mvc.HandleErrorInfo
<h2>Exception details</h2>
<p> Controller: @Model.ControllerName </p>
<p> Action: @Model.ActionName </p>
<p> Exception: @Model.Exception </p>
这篇关于在ASP.NET MVC是什么在我看来,最能展现未处理的异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!