когда возникает ошибка в actionresult, который вызывается запросом ajax, а затем в классе handlemeroratribute для обычного пользователя, я хочу установить статус custom для ответа ajax.
public class MyHandleError : HandleErrorAttribute
{
public override void OnException(ExceptionContext filterContext)
{
filterContext.ExceptionHandled = true;
var ex = filterContext.Exception;
while (ex.InnerException != null)
ex = ex.InnerException;
// if angular made request to action and error raise then responed in
// http status code to tell it that it was a error
if (filterContext.HttpContext.Request.IsAjaxRequest())
{
filterContext.HttpContext.ClearError();
filterContext.HttpContext.Response.Clear();
filterContext.HttpContext.Response.ClearHeaders();
filterContext.HttpContext.Response.ClearContent();
filterContext.HttpContext.Response.StatusCode = 996;
filterContext.HttpContext.Response.StatusDescription = "test";
filterContext.Result = new System.Web.Mvc.HttpStatusCodeResult(996, ex.Message.ToString());
}
else
{
// if user directly made request (like url request) then just
// display them a plane message on screen
filterContext.Result = new RedirectResult("~/Home/Error");
//filterContext.Result = new ContentResult() { Content= ex.Message.ToString() };
}
base.OnException(filterContext);
}
public string r(string a)
{
return a;
}
}
в javascript, который я всегда получал error.status = -1
function exceptionAjax(error) {
console.log('error', error);
//when user is not log in
if (error.status === 999) {
window.location.href = '/account/login?returnUrl=/Accounts/ChartOfAccountsType/Index';
}
// when logged in but not authorized by role
else if (error.status === 998) {
window.location.href = '/account/Denied';
}
// authorized by role but not have permission to use that action
else if (error.status === 997) {
setOperationMessage('You Dont have sufficient privileges to perform this operation', 1);
}
//any other exception raise backend
else if (error.status === 996) {
setOperationMessage(error.statusText, 1);
}
//any other exception raise like 404 not found etc
else {
setOperationMessage('Some thing went wrong', 3);
}
}