ActionFilterAttribute:“取消”在哪里?财产?
ActionExecutingContext 上的 Cancel 属性发生了什么?使用 ActionFilterAttribute 时如何中止 RenderAction 或者是否有另一种方法来给这只猫换皮?
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if(!filterContext.HttpContext.User.Identity.IsAuthenticated)
{
return;
}
base.OnActionExecuting(filterContext);
}
尽管退出了 OnActionExecuting 操作,上面的代码仍继续执行它所应用的 Action?
--- 进一步到原始帖子: 感谢下面的答案,但是,我认为我的上下文还不够清楚,我试图使以下调用无效:
<% Html.RenderAction("Menu", "Shared", new { id = Model.OtherUserId }); %>
当用户未经身份验证时,此操作不应返回任何内容,我可以轻松地放置一个“if”阻止视图,但是,我想将规则保留在控制器中。
Whatever happened to the Cancel property on the ActionExecutingContext? How would one abort a RenderAction when using an ActionFilterAttribute or is there another way to skin this cat?
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if(!filterContext.HttpContext.User.Identity.IsAuthenticated)
{
return;
}
base.OnActionExecuting(filterContext);
}
The code above continues to execute the Action it has been applied to despite exiting the OnActionExecuting operation?
--- Further To original post:
Thanks for the answers below, however, I don't think I have made the context clear enough, I am trying to invalidate the following call:
<% Html.RenderAction("Menu", "Shared", new { id = Model.OtherUserId }); %>
When a user is not authenticated this action should return nothing, I could easily put an 'if' block on the view, however, I would like to keep the rule in the controller.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这很有效,马蒂亚斯,结果是这样的:
This worked great Mattias the result is this:
不,您无法取消动作过滤器的渲染。有很多原因表明您不应该这样做。客户会看到什么?错误页面?没有什么?
我猜你正在构建一个授权操作过滤器,如果你没有登录,它会呈现其他内容。框架中已经有一个(AuthorizeAttribute),如果你没有登录,它会将你重定向到登录页面。他们的方式它在框架中的作用是更改正在执行的结果(filterContext.Result = [[new result]];)。如果您不喜欢它的工作方式,您可以构建自己的实现。
如果您仍然需要取消渲染或类似的操作,您将需要构建自己的 ActionResult 并在 Execute 方法中执行您需要的任何逻辑。
-- 更新 --
如果您想使用渲染操作,您应该将逻辑放入控制器中,如果您未登录,则返回空结果(在框架)。这种逻辑属于控制器操作。
No, you can not cancel a rendering from a action filter. There are many reasons that you should not do that. What would the client see? A error page? Nothing?
I guess you are building a authorize action filter that would render something else if you are not signed in. There is one in the framework already (AuthorizeAttribute) that redirects you to the login page if you are not signed in. The way that they do it in the framework is to change the result that is being executed (filterContext.Result = [[new result]];). If you don't like how it works you can build your own implementation.
If you still need to cancel the rendering or something like that you will need to build your own ActionResult and do whatever logic you need in the Execute method.
-- Update --
If you want to use render action you should just put the logic in the controller and return empty result if you are not signed in (there is a action result called "EmptyResult" in the framework). That kind of logic belongs in the controller action.
马蒂亚斯和贾姆斯特朗已经回答了问题。这是过滤器和控制器的完整代码:
Mattias and rjarmstrong already anwswer question. Here is full code for filter and controller: