I have written one Custom Exception Filter to Log My Application Exception and After exception i want to redirect user to an Error Page Below is my Code
My Code works perfectly fine it catch the exception but after logging it is not throwing me to Error Page, Can you please help
My CustomException Filer class
public class CustomExceptionFilterAttribute : HandleErrorAttribute
{
public override void OnException(ExceptionContext filterContext)
{
try
{
string requestBody = "", Action = "", Controller = "";
try
{
requestBody = filterContext.HttpContext.Request.Form.ToString();
Action = filterContext.RouteData.Values["action"].ToString();
Controller = filterContext.RouteData.Values["controller"].ToString();
}
catch (Exception)
{
}
StringBuilder sbHeader = new StringBuilder();
sbHeader.AppendLine(filterContext.RequestContext.HttpContext.Request.Headers.ToString());
StaticMethods.LogException(SessionHelper.LoginCode.ToString(), Action, Controller, filterContext.Exception.Message, filterContext.RequestContext.HttpContext.Request.RawUrl.ToString(), requestBody, sbHeader.ToString());
// This is which i am more concern about
filterContext.RouteData.Values.Add("Error", filterContext.Exception.Message);
filterContext.Result = new RedirectToRouteResult(
new RouteValueDictionary(new { controller = "Error", action = "Error" }));
}
catch(Exception ex)
{
}
}
}
This is my ErrorController
public class ErrorController : Controller
{
// GET: Error
public ActionResult Error()
{
ViewBag["ErrorMessage"] = RouteData.Values["Error"];
return View();
}
}
This is my Error.cshtml
<div class="alert alert-danger">
<environment names="Development">
<strong>Error!</strong> Some Error Occured.
</environment>
<environment names="Staging,Production">
<strong>Error!</strong> @ViewBag.ErrorMessage
</environment>
</div>
Can someone please help I just want to redirect to error Page after logging exception It is still showing me Yellow Page with thrown error
Thanks