在 ASP.NET MVC 中,我想做类似的事情:
- 让基本控制器检查
ActionResult
. - 如果
ActionResult
是 aViewResult
,则为所有视图加载一些共享数据。 - 如果共享数据满足某些特定条件,则重定向到登录页面。
你将如何实现它?
我考虑了以下问题,但似乎重定向不起作用(由于操作已经执行?)。有没有解决的办法?
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");
}
}