在ASP.NET MVC中,我想做类似的事情:
ActionResult
。ActionResult
是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");
}
}
答案 0 :(得分:0)
我想通过尝试这个找到答案:
filterContext.Result = Redirect("http://someurl");
有效。