我有以下代码在大多数情况下都能正常工作。
但是当会话过期并且用户执行一些AJAX调用时,它无法正常工作,因此它会返回AJAX错误。
如何以正确的方式管理它?
谢谢!
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
public class SessionExpireFilterAttribute : ActionFilterAttribute
{
public static String LoginUrl { get; set; }
public delegate bool CheckSessionDelegate(HttpSessionStateBase session);
public static CheckSessionDelegate CheckSessionAlive;
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
HttpSessionStateBase session = filterContext.HttpContext.Session;
if ((CheckSessionAlive == null) || (CheckSessionAlive(session)))
return;
//send them off to the login page
var url = new UrlHelper(filterContext.RequestContext);
var loginUrl = url.Content(LoginUrl);
session.RemoveAll();
session.Clear();
session.Abandon();
filterContext.HttpContext.Response.StatusCode = 403;
filterContext.HttpContext.Response.Redirect(loginUrl, false);
filterContext.Result = new EmptyResult();
}
}
我目前的解决方案如下,但这是正确的吗?
function UpdateSelectedProduct(uid, checked) {
$lastProductUID = uid;
var params = { uid: uid, isSelected: checked };
$.ajax({
type: "POST",
url: '@Url.Action("UpdateSelectedProduct", "Orders")',
data: JSON.stringify(params),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
},
error: function () {
// alert("Timeout error.");
document.location.href = "/"; // <---- Here it is
}
});
}