user1404577
user1404577

Reputation:

Unable to check Request.IsAjaxRequest() inside my action filter class

I have the following action filter class:-

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class CheckUserPermissionsAttribute : ActionFilterAttribute
    {
        Repository repository = new Repository();
        public string Model { get; set; }
        public string Action { get; set; }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {

string ADusername = filterContext.HttpContext.User.Identity.Name.Substring(filterContext.HttpContext.User.Identity.Name.IndexOf("\\") + 1);
if (!repository.can(ADusername,Model,Action))            {
var viewResult = new ViewResult();
viewResult.ViewName = "~/Views/Errors/Unauthorized.cshtml";
filterContext.Result = viewResult;}
base.OnActionExecuting(filterContext);
}}

But I need to return two views based on the request type, if it is Ajax or not such as:-

    If (Request.IsAjaxRequest()){
    viewResult.ViewName = "~/Views/Errors/_PartialUnauthorized.cshtml";

    }
else{
    viewResult.ViewName = "~/Views/Errors/Unauthorized.cshtml";

but I can not reference Request inside the class ? can anyone advice please ?

Upvotes: 1

Views: 870

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039298

but I can not reference Request inside the class ?

You already did this in the first line of code inside the OnActionExecuting method.

You could retrieve it from the filterContext argument that is passed to your OnActionExecuting method:

bool isAjax = filterContext.HttpContext.Request.IsAjaxRequest();

Upvotes: 2

Related Questions