2013-10-29 41 views
0

我有以下行動過濾器類: -無法檢查Request.IsAjaxRequest()我的行動過濾器類中

[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); 
}} 

但我需要根據請求類型返回兩種觀點,如果是阿賈克斯或者不如: -

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

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

但我不能在類內引用請求?任何人都可以建議嗎?

回答

1

但我不能在類內引用請求?

您已經在OnActionExecuting方法中的第一行代碼中執行了此操作。

你可以從傳遞到您的OnActionExecuting方法filterContext參數檢索:

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