Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
205 views
in Technique[技术] by (71.8m points)

c# - In ASP.NET MVC what is the best show unhandled exceptions in my view?

I have the following in my web.config:

 <customErrors mode="On" defaultRedirect="Error">
  <error statusCode="404" redirect="Error/NotFound" />
</customErrors>

I have a

 [HandleError]

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

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,

Try this syntax:

[HandleError(View="~/Views/Error/Index")]
Public class HomeController : Controller

But what about Logging?!?!?

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.

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) 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) 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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...