我有一个_layout加载菜单:
...
@Html.Action("MenuRole","Menu")
...
在Action MenuRole中我检查与Action Filter的会话:
[CheckSession]
[ChildActionOnly]
public ActionResult MenuRole()
{
....
return PartialView("_LoadMenu",menuModel);
//_LoadMenu is partial view to show menurole
}
在行动过滤器中:
public class CheckSession : ActionFilterAttribute, IActionFilter
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var ctx = filterContext.HttpContext;
//if Session == null => Login page
if (ctx.Session["Username"] == null)
{
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { action = "Index", controller = "Login" }));
}
base.OnActionExecuting(filterContext);
}
}
当会话超时时,_layout在@ Html.Action(“MenuRole”,“Menu”)中显示错误:不允许子操作执行重定向操作
答案 0 :(得分:7)
我有同样的例外,我通过检查IsChildAction
来解决它:
//if Session == null => Login page
if (ctx.Session["Username"] == null && !filterContext.IsChildAction)
{
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { action = "Index", controller = "Login" }));
}
解决了我的问题