开发者

MVC2: Best Way to Intercept ViewRequest and Alter ActionResult

开发者 https://www.devze.com 2022-12-23 04:49 出处:网络
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 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...


First thing, have a gander at Steve Sanderson's Pro ASP.Net MVC book. The section on actionfilters and the controller lifecycle is important to you, mostly b/c there's some special treatment of Authorize filters that 'regular' actionfilters don't get.

Second, you should be able to do this:

filterContext.Result = new RedirectToRouteResult(/*params*/);

and get the effect you want.

0

精彩评论

暂无评论...
验证码 换一张
取 消