I have an web application in .Net MVC 4. The application uses areas and each area have its own custom base controller. I want to concentrate all exceptions in one place and treat each one and redirect to a popup view with a collapsed div with details. Are there some design pattern or example of this?
Asked
Active
Viewed 729 times
0
-
Please clarify - do you want to redirect or not redirect? The title implies a popup on the page *without* a redirect, but the question implies it's ok to redirect. If you forget the popup part, it's very easy and you can put the details in a collapsed div. – freedomn-m Sep 08 '15 at 19:06
-
1In your case, as you do not want to redirect but want a popup, the best method is HandleErrorAttribute. You can return a ViewResult with a modal popup script. – Sep 10 '15 at 02:42
1 Answers
0
Take a look at this article.
There are many ways to do it.
My preferred way is to override on Global.asax, from the link:
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Error(object sender, EventArgs e)
{
Exception exception = Server.GetLastError();
Server.ClearError();
Response.Redirect("/Home/Error");
}
}
In this way, you write the logic only once.
You will probably want to differentiate the errors when the request is made through AJAX though. Should be hard, because you have the Request context there, here is one full solution.

Community
- 1
- 1

Cacho Santa
- 6,846
- 6
- 41
- 73
-
1Is it possible to get the type of exception using Server.GetLastError()? Because, I try this and I cannot be able to get it. For example, if the exception is a IOException or ArgumentException? – Sep 08 '15 at 18:19
-
yes, that should work. I have used that before. What do you mean that you are not able to get it? – Cacho Santa Sep 08 '15 at 18:20
-
1You're right. I got type exception using Server.GetLastError().GetType().FullName . Some exceptions, I received a generic exception like HttpException (when page or controller, action not found) . – Sep 08 '15 at 19:42
-
@user3532133 mark this as the selected answer if it helped then (the green check). – Cacho Santa Sep 08 '15 at 20:18
-
do this tour http://stackoverflow.com/tour of the site, so you can understand better how it works. – Cacho Santa Sep 08 '15 at 20:20