MVC2: Best Way to Intercept ViewRequest and Alter ActionResult
- by Matthew
I'm building an ASP.NET MVC2 Web Application that requires some sophisticated authentication and business logic that cannot be achieved using the out of the box forms authentication.
I'm new to MVC so bear with me...
My plan was to mark all restricted View methods with one or more custom attributes (that contain additional data).
The controller would then override the OnActionExecuting method to intercept requests, analyze the target view's attributes, and do a variety of different things, including re-routing the user to different places.
I have the interception and attribute analysis working, but the redirection is not working as expected.
I have tried setting the ActionExecutingContext.Result to null and even have tried spooling up controllers via reflection and invoking their action methods. No dice.
I was able to achieve it this way...
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.HttpContext.Response.Redirect("/MyView", false);
base.OnActionExecuting(filterContext);
}
This seems like a hack, and there has to be a better way...