System.Web.Mvc.Controller Initialize

你。 提交于 2020-01-02 02:24:08

问题


i have the following 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.


回答1:


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);
    }



回答2:


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);
}


来源:https://stackoverflow.com/questions/1679405/system-web-mvc-controller-initialize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!