ActionResult确定后如何重定向?

时间:2009-05-09 08:07:41

标签: asp.net-mvc redirect actionresult

在ASP.NET MVC中,我想做类似的事情:

  • 让基本控制器检查ActionResult
  • 的类型
  • 如果ActionResultViewResult,请为所有视图加载一些共享数据。
  • 如果共享数据符合某些特定条件,请重定向到登录页面。

你会如何实现?


我考虑过以下内容,但似乎重定向不起作用(由于操作已经执行了?)。有办法解决这个问题吗?

public abstract class BaseController : Controller
{
    protected override void OnActionExecuted
        (ActionExecutedContext filterContext)
    {
        base.OnActionExecuted(filterContext);

        // If the result is a view result,
        // then it loads the shared data (for use in shared view):
        if (filterContext.Result is ViewResult)
            LoadSharedData();
    }

    private void LoadSharedData()
    {
        // TODO: Loads the data that is common for all views.
        // TODO: If the shared data fulfills some specific criteria,
        //       it will redirect to a login page.
        Redirect("http://someurl");
    }
}

1 个答案:

答案 0 :(得分:0)

我想通过尝试这个找到答案:

filterContext.Result = Redirect("http://someurl");

有效。