views:

32

answers:

2

Feel free to close this one if it s a duplicate. I couldn't find an answer.

I wish to be able to place a System.Web.ActionFilterAttribute on an Action Method and override the OnActionExecuting method to insert business logic which determines if the Action should be fulfilled.

Can the ActionExecutingContext be used to cancel the executing Action Method and do one of the following:

  • Send an HTTP Status Code (and the corresponding <customError> page).
  • Execute a different Action Method within the same Controller.
+2  A: 

Send an HTTP Status Code (and the corresponding <customError> page)

Almost:

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
    filterContext.HttpContext.Response.StatusCode = 500;
}

Execute a different Action Method within the same Controller.

Yes:

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
    filterContext.Result = new ViewResult() { ViewName = "SomeOtherAction" };
}
Darin Dimitrov
I don't think the second half of your answer is correct. That will just return a different view, not execute the action.
Ryan
A: 

You can always redirect to another controller/action in an action filter.

See here for an example.

NickLarsen