1

I am new to MVC. I was reading about error logging in MVC.

I came to know about HandleErrorAttribute. I have a question regarding that.

Let's say we placed the HandleErrorAttribute in a Action.

<!-- language: c# --> [HandleError(View = "Error")] public ActionResult Index6() { throw new Exception("Something terrible happened."); return View(); } 

and turned on custom errors in web.config

<customErrors mode="On"> </customErrors> 

Now if any undandled exception occured in that "Index Action", it will show the Error.cshtml which is in "Views/Shared" folder.

But we can have the same behavior just setting some configuration under customErrors section in web.config. like

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

Just to mention, I have a "ErrorController" with "Index" action that returns "Views/Shared/Error.cshtml" view.

So, my question is why should we use HandleErrorAttribute? is there any benefits of using that?

Thanks

1 Answer 1

-2

This post should help getting things cleared

Sign up to request clarification or add additional context in comments.

5 Comments

That post did not help much. It described how to use HandleErrorAttribute to show custom error page instead of "yellow page death". My question was why should one use HandleErrorAttribute to show custom error page instead of configuring Web.config file as I mentioned in the question.
Well you can specify specific error messages for the handler and specify specific views for them..say as in the example specified in the blog: '[HandleError(ExceptionType = typeof(SqlException),View="DatabaseError"][HandleError(ExceptionType = typeof(NullReferenceException),View="LameErrorHandling"]'...Not pretty sure in case this is possible in web.config
yes, That's a good point. I am also not sure if it's possible in web.config. But I don't think we ever need to show different error pages for different types of exception. We can just just show a generic error page to user for any kind of exception. And we can integrate some error logging module like ELMAH to log the actual error what we can later check for the details of the exception that occurred.
Well at the end of the day it all depends on the requirements specified/ project requirements :)
Link only answers are not helpful. You should take the relevant content from the page and add it to your answer (with suitable attribution if you are just copying and pasting).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.