0

I would like to know if its possible to execute few actionresults as a result of an action filter.

filterContext.Result=execution of few actionresults

My problem is that i have to render the view of my action depending on some user credentials or one view of my action + one RenderPartial into this view.

3 Answers 3

1

That should not be done in filter, that kind of decisions should be done in controller, and rendering should be done in view. Injecting partial results in already generated html would be hard and difficult to maintain. Make a sample ViewModel

public class MyViewModel
{
    Model SomeBaseModel; //whatever model is needed for base information
    bool ShouldRenderPartial; //this point is important
}

In the controller, set shouldRenderPartial true or false depending on credentials. and In the view (assuming you use razor syntax)

@if(Model.ShouldRenderPartial){
    @{Html.RenderPartial("PartialViewName")}
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Archil, thanks for your response. I think that in this subject the decision about who should do the job is splitted. For one hand i have some security stuff, that could be handled by an action filter. For other hand i have some "logic view" depending of this security. Actually i have an implementation exactly as your example, but i was wondering if there is any "Best Practice" on this. Thanks. Greets. Jose.
1

You can set the filterContext so that it navigates to another action/view i.e.

private static void SetRedirectToLoginPageForContext(AuthorizationContext filterContext)
        {
            filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary
                                                                 {
                                                                     { "controller", "Login" },
                                                                     { "action", "Index" }
                                                                 });
        }

public class UserAuthenticatedAction : FilterAttribute, IAuthorizationFilter
{
    public void OnAuthorization(AuthorizationContext filterContext)
    {
            SetRedirectToLoginPageForContext(filterContext);
            return;
    }
}

In the above example, I'm setting the filter context so that, upon retuning, the user will be navigated to the Login/Index view.

Have a play around with this code, it should be similar for ActionFilters/GlobalFilters.

3 Comments

Hi Jason, first thinks for your fast response. I dont need to redirect to the user to other controller + action, i just have to show the right view of my action and maybe a RenderPartial on it (depending on security credentials of the user, for this reason i would like to use an action filter if possible). If not i can do the job in the controller and put an if(userdoesnthavecredentials) then RenderPartial, etc.. in the view. Cheers. Jose.
Ahh, no worries. I'll keep my answer on here just in case it's useful. If anyone objects, then I will gladly delete it.
Thanks a lot, it was totally useful. Jose.
0

The solution was more easy that i expected:

First create a multiple action result class that is a wrapper of ActionResult that contains an IEnumerable with ActionResults

/// <summary>
/// this class contains a batch of ActionResult to execute
/// </summary>
public class PortalMultipleActionResult : ActionResult
{
    /// <summary>
    /// Builds a new instance of PortalMultipleActionResult
    /// </summary>
    /// <param name="results"></param>
    public PortalMultipleActionResult(IEnumerable<ActionResult> results)
    {
        Results = results;
    }

    /// <summary>
    ///  Builds a new instance of PortalMultipleActionResult
    /// </summary>
    /// <param name="actions"></param>
    public PortalMultipleActionResult(IEnumerable<Action> actions)
    {
        Results = actions.Select(x => new PortalActionDelegateResult(x));
    }

    /// <summary>
    /// Batch execution of all the results
    /// </summary>
    /// <param name="context"></param>
    public override void ExecuteResult(ControllerContext context)
    {
        foreach (var res in Results)
        {
            res.ExecuteResult(context);
        }
    }

    /// <summary>
    /// Action results collection
    /// </summary>
    private IEnumerable<ActionResult> Results
    {
        get;
        set;
    }
}

Second you can create a Filter in order to return set filterContext.Result to one instance of this PortalMultipleActionResult.

Finally just add the filter to your action method of the controller.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.