문제

Hi I have the following filter attribute that does the trick for controller role authorization:

public class AccessDeniedAuthorizeAttribute : AuthorizeAttribute
    {
        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            base.OnAuthorization(filterContext);

            if (filterContext.HttpContext.Request.IsAjaxRequest())
            {
                // For AJAX requests
                // returninr sample error that need to be catch on ajax success
                filterContext.Result = new JsonResult { Data= new { errorMessage="Access Blocked!!."} };
            }
            else
            {
                if (filterContext.Result is HttpUnauthorizedResult)
                {
                    filterContext.Result = new RedirectResult("~/Account/UnauthorizedRole");
                }
            }


        }
    }

On my client ajax side I have:

$.ajax({
                url: url,
                context: document.body,
                success: function (data) {

                    $('#imgloading').remove();

                },
                error: function (xhr, ajaxOptions, thrownError) {
                    alert('error');
                }
            });

It always generate an error, and the error is a 500. So I dont know what im doing wrong because I just want to get the Data.errorMessage and show it in an alert window.

Any clue on why I'm getting Server Error 500?

도움이 되었습니까?

해결책

First, when you debug, can you verify that this filter is being called and it hits this line?

filterContext.Result = new JsonResult { Data= new { errorMessage="Access Blocked!!."} };

If so, you can use Firebug to inspect the reason for the 500 error. It's possible that it is related to the fact that GET requests are blocked for JsonResults by default.

Can you change the JsonResult line to:

filterContext.Result = new JsonResult { Data= new { errorMessage="Access Blocked!!.", JsonRequestBehavior = JsonRequestBehavior.AllowGet} };
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top