ActionFilterAttribute: Where is the 'Cancel' property?
Asked Answered
B

3

7

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.

Bona answered 26/1, 2010 at 11:48 Comment(1)
See my update on render action.Hecate
H
3

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.

Hecate answered 26/1, 2010 at 12:13 Comment(0)
B
14

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);
    }
Bona answered 26/1, 2010 at 16:31 Comment(2)
but the code inside controller action-method is still being executed?Churinga
OnActionExecuting occurs before the action method executes: msdn.microsoft.com/en-us/library/…Bona
H
3

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.

Hecate answered 26/1, 2010 at 12:13 Comment(0)
W
0

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();
    }

    ...
}
Walling answered 17/3, 2016 at 12:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.