MVC2: Best Way to Intercept ViewRequest and Alter ActionResult

Posted by Matthew on Stack Overflow See other posts from Stack Overflow or by Matthew
Published on 2010-03-19T06:13:35Z Indexed on 2010/03/20 5:11 UTC
Read the original article Hit count: 479

Filed under:
|
|
|

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...

© Stack Overflow or respective owner

Related posts about c#

Related posts about asp.net-mvc2