MVC如何像ActionResult一样返回ActionFilterAttribute中的视图?
是否有可能从 ActionFilterAttribute 返回视图? 现在我只是让它抛出一个错误,并在基本控制器的 OnException 中重新路由到错误页面。当操作中存在权限问题时,我会像我一样显示我的 NoAccess 视图,但它是从操作的属性中发生的。
public sealed class UserHasPermissionAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if(HasPermission == false){
//What are my options here a view?
}
}
}
看过几个网站和博客,但没有一个很好地解释了我正在寻找的内容。
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 filterContext)
{
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从您使用操作过滤器的声音来看,听起来也许您应该使用
AuthorizeAttribute
?或者,您可以尝试在 OnActionExecuting 方法本身中进行重定向,使用类似以下内容:
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: