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.
I do something similar to maxlego which handles all errors (not just those occurring in controllers with the HandleError attribute).
My MvcApplication class (in global.asax.cs) has this:
The above code uses an extension method from my MVC library of useful stuff. With this in place I don't need any error handling attributes, customErrors config or custom filters. Instead the extension method will log the error details then invoke an appropriate view, either:
The extension method code to make this work is:
Note, the above uses NLog for logging the error details but could easily be changed to support something else. Also, this method respects your IoC container when resolving the ErrorController.
I have used this little piece of code to show users handled errors page. Whether page was not found or some other error occurred.
And errors controller look something like this. If you need detailed exception, it is accessible via RouteData
You can add different view for each http code. Just implement action Http{Code}
I can see the misconception. You want to do the
MVC
thing andredirect
to a controller action.But
defaultRedirect
is itself aWeb Form
convention and thereby limited. The moment you redirect to another controller, you will lose yourHttpContext
, and thereby lose yourHandleErrorInfo
ObjectYour
[HandleError]
Attribute requires aView
to direct its error message to. Going by your example above, I assume that you have aViews/Error
Folder for yourErrorController
, and in it you have anIndex
View. If you want to your Filter Context to send aHandleErrorInfo
object to that view,Try this syntax:
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] attribute2) 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