I need to throw an HttpException during an AjaxRequest in Controller and CustomFilterAttribute
When I throw an Exception in Controller with error 403
[HttpPost] [CustomAuthorize] public ActionResult AjaxSelectBinding() {
In the client script, I always get the result code - 500
$.ajax({ type: 'POST', url: '/Groups/AjaxSelectBinding', success: function(data) { }, error: function (xhr, ajaxOptions, thrownError) {
How can I throw an HttpException in my FilterAttribute and get this code on the client page. I try to do this, but I get a 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 to throw an Exception on FilterAttribute , I get a 500 status code again
source share