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

查看:106
本文介绍了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派生,它会重定向他们,如果某个值为true。然而,这code不工作!到RedirectToAction呼叫被提出,但初始化方法完成后,它只是移动到原来叫控制器。

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天全站免登陆