System.Web.Mvc.Controller初始化 [英] System.Web.Mvc.Controller Initialize

查看:621
本文介绍了System.Web.Mvc.Controller初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下基本控制器...

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

    }

}

我的控制器将派生自BaseController,如果某个值为真,它将重定向它们。但是,这段代码不工作!对RedirectToAction的调用是做的,但是在Initialize方法完成后,它只会移动到原来调用的控制器。

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.

这是有道理的吗

非常感谢,

ETFairfax。

ETFairfax.

推荐答案

我想你是凌驾错误的方法。尝试使用OnActionExecuting或OnActionExecuted。

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

这篇关于System.Web.Mvc.Controller初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆