ActionResult确定后如何重定向?
在 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");
}
}
In ASP.NET MVC I would like to do something like:
- Let a base controller check for the type of the
ActionResult
. - If the
ActionResult
is aViewResult
, load some shared data for all views. - If the shared data fulfills some specific criteria, redirect to a login page.
How would you implement that?
I thought about the following, but it seems the redirect does not work (due to the action has already been executed?). Is there a way around this?
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");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想我通过尝试找到了答案:
它有效。
I think I found an answer by trying this:
It works.