I need to throw HttpException
during AjaxRequest
in Controller
and CustomFilterAttribute
When I throw Exception
in Controller
with 403
error
[HttpPost]
[CustomAuthorize]
public ActionResult AjaxSelectBinding()
{
// 403 Error code
throw new HttpException((int)HttpStatusCode.Forbidden, "Forbidden");
}
In client script I always get the result code - 500
$.ajax({
type: 'POST',
url: '/Groups/AjaxSelectBinding',
success: function(data) {
},
error: function (xhr, ajaxOptions, thrownError) {
// HERE I GET ALWAYS 500 ERROR CODE
}
});
How can I throw HttpException
in my FilterAttribute
and get this code in client page. I try do this, but I get 200
status code:
public class CustomAuthorize : AuthorizeAttribute
{
public override void OnAuthorization(AuthorizationContext filterContext)
{
base.OnAuthorization(filterContext);
SharedControllerBase ctrl = (SharedControllerBase)filterContext.Controller;
if (!ctrl.User.Identity.IsAuthenticated &&
filterContext.HttpContext.Request.IsAjaxRequest())
{
filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
filterContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
}
}
When I try throw Exception
in FilterAttribute
I get 500
Status Code again