title: How to redirect to error page from onexception method instead of returning to jQuery/Ajax call in c# mvc
body: <p>I'm trying to redirect to an Error page inside onexception method when an exception occurs. But the thing is I have Ajax function, so even if I redirect inside onexception class, it does not redirect to error page and it always end up with executing Ajax function. Please can anyone suggest a solution for this.</p>
  
  <p>This is my controller method and when exception throws, it will call to base controller on exception method.</p>
  
  <pre><code> public ActionResult DeleteConfirmed(int id)
          {
              try
              {
  
                  string displayMessage = string.Empty;
                  //Delete ward details and check successfulness of the function and return json result
                  if (wardManager.DeleteWard(id) == true)
                  {
                      displayMessage = CustomEnumMessage.GetStringValue(ConfirmationMessages.ConfirmationErrorMsg);
                      return Json(new { Message = displayMessage });
                  }
                  //Return json result if unsuccessfull 
                  else
                  {
                      displayMessage = CustomEnumMessage.GetStringValue(ConfirmationMessages.ConfirmationRemovedMsg);
                      return Json(new { Message = displayMessage });
                  }
  
              }
              catch (System.Exception ex)
              {
                  throw ex;
              }
  
          }
  </code></pre>
  
  <p>This is my base controller onexception method</p>
  
  <pre><code>protected override void OnException(ExceptionContext filterContext)
          {
              //Get exception type
              System.Type ExceptionType = filterContext.Exception.GetType();
  
  
              if (ExceptionType.Name == "DbUpdateException")
              {
                  ViewData["ErrorMessage"] = filterContext.Exception.InnerException.Message;
                  this.View("DatabaseException", filterContext.Exception).ExecuteResult(this.ControllerContext);
  
              }
              else
              {
                  ViewData["ErrorMessage"] = filterContext.Exception.Message;
                  this.View("ApplicationException", filterContext.Exception).ExecuteResult(this.ControllerContext);
              }
  
  }
  </code></pre>
  
  <p>This is my Ajax function</p>
  
  <pre><code>$.ajax({
                      type: "POST",
                      dataType: "json",
                      jsonpCallback: "onJSONPLoad",
                      url: '@Url.Action("Delete")',
                      data: $('#form').serialize(),
                      success: function (data) {
                          $('#submit').hide();
                          TriggerMsg(data.Message);
                      },
                      error: function (xhr) {
                          if (xhr.getResponseHeader('Content-Type').indexOf('application/json') &gt; -1) {
                              var json = $.parseJSON(xhr.responseText);
                              alert(json.errorMessage);
                          }
  
                      }
                  });
  </code></pre>
  
  <p>I built this Ajax function for only displaying Successful messages and it avoids me to redirecting to error page. The problem is even if i redirect to error page inside onexception method, finally fire into Ajax error: function and it does not redirect into DatabaseException or ApplicationException views. Can anyone suggest a solution for this issue.  </p>
  
  <p>Thanks</p>