在 ASP.NET MVC 中,我认为最好的显示未处理的异常是什么? [英] In ASP.NET MVC what is the best show unhandled exceptions in my view?

查看:36
本文介绍了在 ASP.NET MVC 中,我认为最好的显示未处理的异常是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 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.

我确定这与错误在重定向中丢失有关,所以我想看看我是否遗漏了什么,以及是否有人提出建议,我可以在其中获得显示 Stacktrace 和错误消息的视图.

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 事情和 redirect 到控制器操作.

I can see the misconception. You want to do the MVC thing and redirect to a controller action.

但是defaultRedirect 本身就是一个Web Form 约定,因此受到限制.当你重定向到另一个控制器时,你将失去你的 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] 属性需要一个 View 将其错误消息定向到.按照上面的示例,我假设您有一个 ErrorControllerViews/Error 文件夹,其中有一个 Index 视图.如果你想让你的过滤器上下文发送一个 HandleErrorInfo 对象到那个视图,

Your [HandleError] Attribute requires a View to direct its 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) 覆盖基本控制器的 On Exception 方法;创建您自己的 Controller 继承自 MVC Controller 类,但覆盖 On Exception Method.这种方式可以与[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(order=2)] 结合使用,因为过滤器属性可以采用应用优先级的顺序参数.

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 询问错误视图会是什么样子.

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屋!

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