1

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 ?

4

1 回答 1

2

但我不能在类内引用 Request ?

您已经在OnActionExecuting方法内的第一行代码中执行了此操作。

您可以从filterContext传递给您的OnActionExecuting方法的参数中检索它:

bool isAjax = filterContext.HttpContext.Request.IsAjaxRequest();
于 2013-10-29T14:06:36.407 回答