【发布时间】:2017-02-10 03:21:12
【问题描述】:
我有一个MVC ASP.NET 项目并具有以下操作过滤器属性
public override void OnActionExecuting(System.Web.Mvc.ActionExecutingContext filterContext)
{
HttpContext ctx = HttpContext.Current;
if (ctx.Session != null)
{
if (ctx.Session.IsNewSession)
{
string sessionCookie = ctx.Request.Headers["Cookie"];
if ((null != sessionCookie) && (sessionCookie.IndexOf("ASP.NET_SessionId") >= 0))
{
if (filterContext.HttpContext.Request.IsAjaxRequest())
{
filterContext.Result = new JsonResult { Data = "Session Timeout. Redirecting...", ContentType = "application/json" };
filterContext.HttpContext.Response.StatusCode = 440;
}
else
{
filterContext.Result = new RedirectToRouteResult(
new RouteValueDictionary
{
{ "Controller", "Error" },
{ "Action", "SessionTimeout" }
});
}
}
}
}
}
我要做的是重定向到SessionTimeout 操作,该操作会在 IIS 会话超时时显示错误页面。
大多数情况下它运行良好,但我发现有时,尤其是我第一次启动应用程序时,此代码会运行到 else 块,从而重定向到错误页面这是不需要的,因为这是用户的第一次访问。
(这里的“第一次”表示距离上次运行应用程序有足够长的时间,比如几天)
遇到else 块,这意味着会话是一个新会话,但也存在ASP.NET_SessionId cookie。
我的问题是:为什么会这样?这种行为是否正常且符合预期?
(我不需要解决方案,只需对行为进行一些解释就可以了……)
【问题讨论】:
标签: c# asp.net asp.net-mvc cookies iis-7