1

I have a simple LogIn form, which on LogIn button clicked performs ajax call to ActionResult LogIn(LogInRequest request) in AccountController that returns JsonResult. I have CustomHandleErrorAtribute that inherits from HandleErrorAttribute and redirects to CustomErrorPage.cshtml. Somwhere in the ActionResult exception is thrown, that should be handled by CustomHandleErrorAtribute and it is handled. Then the CustomErrorPage action is executed but it doesnt actualy return the view. It stays on same LogInPage.

My LogIn action in AccountsController

[AllowAnonymous]
[HttpPost]
[CustomAttributes.CustomHandleError]
public ActionResult Login(LogInRequest request)
{
    StandartResponse finalResult = new StandartResponse();
    //some code that trows exception
    return new JsonResult() { Data = finalResult.Result};
}

My Ajax call on LogIn button clicked:

function LogInUser(s, e) {
var example = { username: UserName.GetValue(), password: Password.GetValue() };
$.ajax({
    contentType: "application/json; charset=utf-8",
    dataType: 'json',
    type: "POST",
    url: "/Account/LogIn",
    data: JSON.stringify({ request: example }),
    success: function (data) {

        jQuery.ajaxSettings.traditional = true;
        if (data.Status == InfoType.Success) {
            alert('success');
            var url = "/Home/Index";
            window.location.href = url;
        }
        else {
            alert('here');
            var result = JSON.stringify(data.Infoes);
            popUpErrorMessagePartial.PerformCallback({ message: result });
            popUpErrorMessagePartial.Show();
        }
    },
    error: function(){
       alert('error');
    }
   });
}

CustomHandleErrorAttribute:

public override void OnException(ExceptionContext filterContext)
{


        filterContext.ExceptionHandled = true;
        filterContext.Result =
        new RedirectToRouteResult(new RouteValueDictionary
               {
                   { "action", "ErrorUnauthorised" },
                   { "controller", "CustomErrorPages" },
                   { "Area", String.Empty }
               });
        filterContext.HttpContext.Response.Clear();
        filterContext.HttpContext.Response.StatusCode = 500;

}

After debugging the Action of CustomErrorPage is executed, but then the error in the ajax call is executed, the allert is displayed and no redirection is performed. Do you know how to handle this?

Gamaboy
  • 117
  • 11

1 Answers1

2

Because you do get an exception, the ajax call itself fails, which is why you get the alert, but you can't redirect to another page inside the server side of an ajax call. Instead you can put the redirect inside the fail block of your ajax call. Something like....

error: function(){
   window.location = <put error page url here>
   alert('error');
}
Bacskay
  • 396
  • 4
  • 12
  • Thank you, I will mark your response as the answer. For those who struggle in this, i found possible solution here with CustomAjaxHandleErrorAtribute that returns Json result to the error function of the ajax call: https://stackoverflow.com/questions/9298466/can-i-return-custom-error-from-jsonresult-to-jquery-ajax-error-method – Gamaboy Aug 24 '17 at 13:48