0

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

  • 让基本控制器检查ActionResult.
  • 如果ActionResult是 a ViewResult,则为所有视图加载一些共享数据。
  • 如果共享数据满足某些特定条件,则重定向到登录页面。

你将如何实现它?


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

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

1 回答 1

0

我想我通过尝试找到了答案:

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

有用。

于 2009-05-09T08:11:55.767 回答