ActionFilterAttribute:“取消”在哪里?财产?

发布于 2024-08-19 08:49:39 字数 678 浏览 9 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凌乱心跳 2024-08-26 08:49:39

这很有效,马蒂亚斯,结果是这样的:

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
        {
            filterContext.Result = new EmptyResult();
            return;
        }
        base.OnActionExecuting(filterContext);
    }

This worked great Mattias the result is this:

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
        {
            filterContext.Result = new EmptyResult();
            return;
        }
        base.OnActionExecuting(filterContext);
    }
空城仅有旧梦在 2024-08-26 08:49:39

不,您无法取消动作过滤器的渲染。有很多原因表明您不应该这样做。客户会看到什么?错误页面?没有什么?

我猜你正在构建一个授权操作过滤器,如果你没有登录,它会呈现其他内容。框架中已经有一个(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.

说谎友 2024-08-26 08:49:39

马蒂亚斯和贾姆斯特朗已经回答了问题。这是过滤器和控制器的完整代码:

public class CancelFilter : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        //before execution
        var id = filterContext.RequestContext.HttpContext.Request.Params["id"];
        if (id == "0")
        {
            filterContext.Result = new EmptyResult();
            return;
        }
        base.OnActionExecuting(filterContext);
    }

    public override void OnResultExecuted(ResultExecutedContext filterContext)
    {
        //after execution
    }
}


[CancelFilter]
public class HomeController : Controller
{
    public ActionResult DoSome(string id)
    {
        return View();
    }

    ...
}

Mattias and rjarmstrong already anwswer question. Here is full code for filter and controller:

public class CancelFilter : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        //before execution
        var id = filterContext.RequestContext.HttpContext.Request.Params["id"];
        if (id == "0")
        {
            filterContext.Result = new EmptyResult();
            return;
        }
        base.OnActionExecuting(filterContext);
    }

    public override void OnResultExecuted(ResultExecutedContext filterContext)
    {
        //after execution
    }
}


[CancelFilter]
public class HomeController : Controller
{
    public ActionResult DoSome(string id)
    {
        return View();
    }

    ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文