开发者

MVC How to return view in ActionFilterAttribute like ActionResult?

开发者 https://www.devze.com 2023-04-10 02:25 出处:网络
Is it even possible to return a view from an ActionFilterAttribute? Right now i just have it throw an error and in the OnException in the base controller reroutes to an error page. Would liek to show

Is it even possible to return a view from an ActionFilterAttribute? Right now i just have it throw an error and in the OnException in the base controller reroutes to an error page. Would liek to show my NoAccess view like i do when theres an permissions problem inside an action, but have it occur from an attribute on the action.

    public sealed class UserHasPermissionAttribute : ActionFilterAttribute
    {
      public override void OnActionExecuting(ActionExecutingContext filt开发者_运维问答erContext)
      {
if(HasPermission == false){

//What are my options here a view?

}
}
}

Seen several sites an blogs, but none have explained well, or what i am looking for.


By the sounds of what you're using your action filter for, it sounds like perhaps you should be using the AuthorizeAttribute?

Alternatively you could try just redirecting in the OnActionExecuting method itself, using something like:

filterContext.HttpContext.Response.Redirect(errorUrl);
0

精彩评论

暂无评论...
验证码 换一张
取 消