如何重定向我的动作过滤器类以返回视图

问题描述 投票:0回答:1

我具有以下用于实现自定义授权系统的类:-

[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))            {
                filterContext.Result = new HttpUnauthorizedResult("You cannot access this page");


            }

            base.OnActionExecuting(filterContext);
        }
    }

但是当前使用Firefox访问我的Web应用程序时,如果返回filterContext.Result = new HttpUnauthorizedResult("You cannot access this page");,则用户将不断获得提示窗口,提示您永远输入用户名和密码。因此,有一种方法可以修改我的操作方法,以便返回到显示诸如“您无权执行此操作。”之类的消息的视图,而不是返回filterContext.Result = new HttpUnauthorizedResult("You cannot access this page");。谢谢

asp.net asp.net-mvc
1个回答
1
投票

您可以返回ViewResult

ViewResult

您当然可以在该视图结果实例上设置其他属性,例如布局和视图所需的视图模型。

© www.soinside.com 2019 - 2024. All rights reserved.