ActionFilterAttribute: Where is the Undo property?

Whatever happens to the Cancel property in an ActionExecutingContext? How can I interrupt RenderAction when using ActionFilterAttribute or is there any other way to trick this cat?

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

Does the above code continue to perform the action to which it was applied despite exiting the OnActionExecuting operation?

--- In addition to the original post: Thanks for the answers below, however, I don’t think I made the context clear enough, I am trying to invalidate the following call:

 <% Html.RenderAction("Menu", "Shared", new { id = Model.OtherUserId }); %> 

If the user is not authenticated, this action should not return anything, I could easily place the "if" block in the view, however I would like to save the rule in the controller.

+6
override model-view-controller renderaction actionfilterattribute
source share
3 answers

No, you cannot cancel the rendering from the action filter. There are many reasons why you should not do this. What will the client see? Error page? Nothing?

I assume that you are creating an authorization authorization filter that will display something else if you are not logged in. Within the framework there is already one (AuthorizeAttribute), which redirects you to the login page if you are not logged in. what they do is within the framework is to change the result that is executed (filterContext.Result = [[new result]];). If you do not like how this works, you can create your own implementation.

If you still need to cancel the rendering or something similar, you will need to create your own ActionResult and execute any logic that you need in the Execute method.

- Update -

If you want to use a visualization action, you just have to put the logic in the controller and return an empty result if you are not logged in (the structure contains the result of the action called "EmptyResult"). This logic belongs to the action of the controller.

+3
source share

This worked great Mattias, the result is the following:

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

Matthias and rjarmstrong have already answered the question. Here is the complete code for the 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(); } ... } 
0
source share

All Articles