【发布时间】:2014-01-24 23:40:59
【问题描述】:
我需要在用户会话过期并重定向到登录页面时显示警报。 实际上我已经创建了一个应用在控制器上的动作过滤器并检查 如果会话过期,如果 es 重定向到登录页面,否则它让操作完成 成功。
它工作正常,但是当我发出 ajax 请求时出现了我的问题。 例如,我的视图中有 ajax.beginform,并且我已经在该操作上保存了代码 方法。现在,假设用户在会话过期时提交了表单, 在保存方法运行之前,我的 actionFilter 被调用,因为我已将其设置为 控制器,所以,从那里它说,重定向到登录页面。 但我从控制器返回 Json,它不会重定向。
我应该如何实现这些东西?
我的代码是::::::::::::
对于每个控制器,我的动作过滤器以相同的方式定义,我只为一个控制器定义所以,假设下面是我的控制器。
[CheckSessions]
public class MyController : Controller
{
public ActionResult Add(Model model)
{
Insert code and returns
return Json(new { Msg = Message});
}
}
Action Filter is here
public class CheckSessions: ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (HttpContext.Current.Session["LoginSession"] == null && HttpContext.Current.Session["LoginSession"] == null)
{
HttpContext.Current.Response.Redirect("/LoginController/Login");
}
}
}
In View Page:::
@using (Ajax.BeginForm("Add", "MyController ", new AjaxOptions { OnSuccess = "MessageConfirmation" }))
{
Content goes here
}
<script>
Getting Response
function MessageConfirmation(Json) {
alert(Json.StatusCode);
if(Json.StatusCode==404)
{
redirect to login
}
}
</script>
【问题讨论】:
标签: c# asp.net .net asp.net-mvc-3 asp.net-mvc-4