我发现这段代码在我的会话到期时会被捕获:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace SuburbanCustPortal.MiscClasses
{
public class SessionExpireFilterAttribute : ActionFilterAttribute {
public override void OnActionExecuting( ActionExecutingContext filterContext ) {
HttpContext ctx = HttpContext.Current;
// check if session is supported
if ( ctx.Session != null ) {
// check if a new session id was generated
if ( ctx.Session.IsNewSession ) {
// If it says it is a new session, but an existing cookie exists, then it must
// have timed out
string sessionCookie = ctx.Request.Headers[ "Cookie" ];
if ( ( null != sessionCookie ) && ( sessionCookie.IndexOf ( "ASP.NET_SessionId" ) >= 0 ) )
{
ctx.Response.Redirect("~/Home/Login");
}
}
}
base.OnActionExecuting ( filterContext );
}
}
}
它没有问题但是被叫动作仍在运行它的过程:
[Authorize]
[SessionExpireFilter]
public ActionResult PrePayment(PaymentModel.PrePayment model)
{
if (string.IsNullOrWhiteSpace(Session[SessionEnums.CurrentAccountGuid.ToString()].ToString()))
{
return RedirectToAction("AddCustomer", "Customer");
}
if (TempData["ViewData"] != null)
{
ViewData = (ViewDataDictionary) TempData["ViewData"];
}
...
[SessionExpireFilter]
正在运行此代码,但随后会继续使用该方法的其余部分,因为会话不再有效而导致问题。我在PrePayment
的第一行收到错误,因为它正在返回。
重定向不应该阻止这种情况发生吗?
编辑**
我做了修改建议,现在有了:
命名空间SuburbanCustPortal.MiscClasses { public class SessionExpireFilterAttribute:ActionFilterAttribute {
public override void OnActionExecuting( ActionExecutingContext filterContext )
{
HttpContext ctx = HttpContext.Current;
// check if session is supported
if ( ctx.Session != null ) {
// check if a new session id was generated
if ( ctx.Session.IsNewSession ) {
// If it says it is a new session, but an existing cookie exists, then it must
// have timed out
string sessionCookie = ctx.Request.Headers[ "Cookie" ];
if ( ( null != sessionCookie ) && ( sessionCookie.IndexOf ( "ASP.NET_SessionId" ) >= 0 ) )
{
//ctx.Response.Redirect("~/Account/LogOn/");
filterContext.Result = new RedirectResult("~/Account/LogOn");
return;
}
}
}
base.OnActionExecuting ( filterContext );
}
}
}
AccountScreen正在调用预付款,似乎在返回后正在重新加载。
答案 0 :(得分:3)
使用此:
filterContext.Result = new RedirectResult("~/Home/Login");
return;
而不是:
ctx.Response.Redirect("~/Home/Login");