2

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

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Parshuram Kalvikatte
  • 1,616
  • 4
  • 20
  • 40
  • Both answers below are 2 ways of doing this, you can use web.config transforms https://stackoverflow.com/questions/5811305/web-config-debug-release with the latter as well – Harry Jan 17 '18 at 10:19

5 Answers5

5

Don't understand why you want to use ErrorAttribute. You could easily use Global.asax file for application level errors.

 protected void Application_Error(object sender, EventArgs e)
        {
            Exception exception = Server.GetLastError();
            //Log your exception here
            Response.Clear();
            string action= "Error";
            // clear error on server
            Server.ClearError();

            Response.Redirect(String.Format("~/Error/{0}?message={1}", action, exception.Message));

        }
Abdul Rehman Sayed
  • 6,532
  • 7
  • 45
  • 74
0

if you want to redirect it to another page, you need to specify the directory.

return View();

This basically means return to the current page with the error viewbag. You can try

return redirect("~/Error"); //return redirect("url of the page")
Rey Norbert Besmonte
  • 791
  • 2
  • 12
  • 29
0

You need to add your error page in web.config

<system.web>
  <customErrors mode="On" defaultRedirect="~/Error/Index">
    <error statusCode="500" redirect="~/Error/YourPage"/>             
  </customErrors>
<system.web/>
0

this is a suggestion. would you tried doing this.

filterContext.Result = new RedirectToRouteResult(
                                       new RouteValueDictionary { { "Error", "Error" } });
0

In ASP.NET MVC 5, you can use this:

if(/*Some error*/){
    return View("Error");
}
César León
  • 2,941
  • 1
  • 21
  • 18