views:

328

answers:

2

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.

+2  A: 

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 Jakobsson
+3  A: 

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