开发者

System.Web.Mvc.Controller Initialize

开发者 https://www.devze.com 2022-12-10 09:13 出处:网络
i have the followi开发者_开发知识库ng base controller... public class BaseController : Controller

i have the followi开发者_开发知识库ng base controller...

public class BaseController : Controller
{

    protected override void Initialize(System.Web.Routing.RequestContext requestContext)
    {

        if (something == true)
            RedirectToAction("DoSomething", "Section");

        base.Initialize(requestContext);

    }

}

Basically, all my controllers will derive from BaseController, and it will redirect them if a certain value is true. However, this code does not work!!! The call to RedirectToAction is made, but after the Initialize method is finished, it will just move on to the originally called controller.

Does that make sense??

Many thanks,

ETFairfax.


I think you are overriding wrong method. Try with OnActionExecuting or OnActionExecuted.

protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
      if (something == true)
          filterContext.Result =  RedirectToAction("DoSomething", "Section");
      else
          base.OnActionExecuting(filterContext);
    }


I'm not sure if this is what you want, but try this:

protected override void Initialize(System.Web.Routing.RequestContext requestContext)
{
    if (something == true)
        RedirectToAction("DoSomething", "Section");
    else
        base.Initialize(requestContext);
}
0

精彩评论

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