是否覆盖OnActionExecuting以测试请求,用户是否有权进行呼叫操作?

Nur*_*MAZ 3 asp.net-mvc-3

我想重写控制器OnActionExecuting方法,并且我将检查操作和控制器用户是否正确。

    public class BrowseController : Controller
    {
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            //TODO: ask service user has right this action.
            //string actName = filterContext.ActionDescriptor.ActionName;
            //string cntName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName
            //foo(actName, cntName, userInfo) if return false go NoAccess page!
            //filterContext.Result = new RedirectResult("_NoAccessRight");

            base.OnActionExecuting(filterContext);
     }
   }
Run Code Online (Sandbox Code Playgroud)

我想在验证用户权限之后

filterContext.Result = new RedirectResult("_NoAccessRight");
Run Code Online (Sandbox Code Playgroud)

但是我无法获得页面“ _NoAccessRight”以及〜/ shared / _NoAccessRight”

你能给个主意吗?谢谢。

Bui*_*ted 6

为了帮助您以后

public class BrowseController : Controller
{
    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        filterContext.Result = new RedirectToAction("NoAccessRight");
   }

   public ActionResult NoAccessRight() 
   {
       return View("_NoAccessRight");
   }
}
Run Code Online (Sandbox Code Playgroud)

  • 从RedirectToAction(“ NoAccessRight”)(至少在MVC3中)省略“新”。 (4认同)