0

I am using one MVC application where i have to handle all exception occurs in the code. I have found about exception filter and implemented there. Below is the created exception filter code:

public class HandleException : HandleErrorAttribute
{
        #region Log Initialization
        FileLogService logService = new 
        FileLogService(typeof(HandleException));
        #endregion
        public override void OnException(ExceptionContext filterContext)
        {
            filterContext.ExceptionHandled = true;
            Log(filterContext.Exception);
            base.OnException(filterContext);
        }

        private void Log(Exception exception)
        {
            logService.Error(exception.ToString());
        }
 }

Now i used this filter as attribute in my controller like below:

[AuthSession]
    [HandleException]
    public class OrganizationalController : BaseController
    {
       
        public ActionResult OrgSummary()
        {
            try
            {
                int a = 1, b = 0;
                int result = a / b;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            ViewData["ShowGrid"] = false;
            return View();
        }
   }

As you can see in above code i am trying to generate exception in the code. In catch exception block when i used throw keyword then exception filter getting executed else not.

Now i need here when any exception occurs in the application i need to show a custom popup message for user. In popup message once user click on ok button then user should be available on the same page. The page should not break or get blank.

How could i implement this functionality?

V.Prasad
  • 131
  • 4
  • 19
  • use custom error page and in catch send to that partial page or popup in your case – Arijit Mukherjee Dec 28 '17 at 11:39
  • I think i answer this question in another post: https://stackoverflow.com/questions/35539341/custom-exceptions-to-http-status-codes-in-asp-net-api/35539558#35539558 – Orel Eraki Dec 28 '17 at 11:52
  • Hi Arijit, Thanks for your quick reply. My question is here if exception filter is not getting called without use of try catch block in action method then what is the use of it. As i have created one another filter to check user session is active or not, So in each request this filter is getting fired and checking this issue. But in exception filter there is no same case. i have to write try catch block in each action method and use there throw keyword then this filter is hitting. If this is the case then what is the use of this filter then. – V.Prasad Dec 28 '17 at 11:58
  • Hi Arijit, Sorry actually i had used one example from web and tried to implement there now i removed the try catch block in action method and using a code to generate exception divide by zero exception in this case now exception filter is hitting and getting the exact exception there. But after that my page is complete blank. Here i need instead of complete blank page it should be the same state as it was there before caught exception. – V.Prasad Dec 28 '17 at 12:04

1 Answers1

1

Try this code. May be it helps

public class MyExceptionFilter: FilterAttribute, IExceptionFilter   
 {  
public void OnException(ExceptionContext filterContext)   
{  
     // below code will redirect to the error view
        filterContext.Result = new RedirectResult("ErrorPage.html");  
        filterContext.ExceptionHandled = true;  

}  
}

and then you need to apply the above as an attribute to your action methods like: [MyExceptionFilter]
public ActionResult XYZ() { }

  • Hi Pooja, This code i have already. in this code what happens if any page found exception then process will call this filter and redirect to this html page to user where again user has to come to previous page as he requested before. Is there in exception filter can we return partial view? – V.Prasad Dec 29 '17 at 09:12
  • Pooja, i was missing something from your provided code. When i followed correctly then found my issue got resolved. – V.Prasad Jul 16 '19 at 15:02