Search code examples
asp.net-mvcasp.net-mvc-3action-filter

MVC Action Filters - What Causes the Controller Code to not execute?


If I define an Action filter that implements IActionFilter like so:

public class FooAttribute : FilterAttribute, IActionFilter
{
    public void OnActionExecuted(ActionExecutedContext filterContext)
    { }

    public void OnActionExecuting(ActionExecutingContext filterContext)
    {
        filterContext.Result = new ContentResult 
        { 
            Content = "Test", 
            ContentEncoding = Encoding.UTF8, 
            ContentType = "text" 
        };
    }
}

And apply it to the following action method:

[Foo]
public ActionResult Index()
{
    return View();
}

When I do this the Index action code does not run and neither does the OnResultExecuted method. Is this because I set the Result property in the OnActionExecuting method? Are there any other things that will cause the normal flow to be interrupted?


Solution

  • I think its just the Result property that has this effect.. See here : http://books.google.be/books?id=gzfFQrs_qQAC&lpg=PA442&ots=EXCefpt5-O&dq=iactionfilter%20onactionexecuting&pg=PA442#v=onepage&q=iactionfilter%20onactionexecuting&f=false

    User can cancel the action by setting Result to something non-null