Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在MVC4中检查调用控制器构造函数时的会话超时_C#_Asp.net Mvc 4_Session - Fatal编程技术网

C# 在MVC4中检查调用控制器构造函数时的会话超时

C# 在MVC4中检查调用控制器构造函数时的会话超时,c#,asp.net-mvc-4,session,C#,Asp.net Mvc 4,Session,我正在使用以下方法处理MVC 4应用程序中的会话到期: 步骤1:创建以下类: [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)] public class SessionExpireFilterAttribute : ActionFilterAttribute { public override void OnActio

我正在使用以下方法处理MVC 4应用程序中的会话到期:

步骤1:创建以下类:

    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
public class SessionExpireFilterAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        HttpContext ctx = HttpContext.Current;

         string controllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName.ToLower();
         string actonName = filterContext.ActionDescriptor.ActionName.ToLower();
         if (!(controllerName.Contains("account")||(controllerName.Contains("home") && actonName.Contains("index"))))
         {

             // If the browser session or authentication session has expired...
             if (SessionManager.Instance["PlatformId"] == null || !filterContext.HttpContext.Request.IsAuthenticated)
             {
                 if (filterContext.HttpContext.Request.IsAjaxRequest())
                 {

                     filterContext.Result = new JsonResult { Data = "_Logon_" };
                 }
                 else
                 {

                     filterContext.Result = new RedirectToRouteResult(
                         new RouteValueDictionary {
                    { "Controller", "Home" },
                    { "Action", "TimeoutRedirect" }
            });
                 }
             }
         }
        base.OnActionExecuting(filterContext);
    }
}
步骤2:将其添加到RegisterGlobalFilter,如下所示:

    filters.Add(new SessionExpireFilterAttribute());
     public class DashboardController : BaseController
{
    private DashboardService dashboardService;


    public DashboardController()
    {

        dashboardService = new DashboardService(this.DbContext, (int)SessionManager.Instance["PlatformId"]);

    }
 }
我在会话处于活动状态且工作正常的情况下测试了它——在执行每个操作时,它检查会话是否处于活动状态。但问题是我正在使用会话值初始化构造函数中的某些对象,如下所示:

    filters.Add(new SessionExpireFilterAttribute());
     public class DashboardController : BaseController
{
    private DashboardService dashboardService;


    public DashboardController()
    {

        dashboardService = new DashboardService(this.DbContext, (int)SessionManager.Instance["PlatformId"]);

    }
 }
当会话超时时,会在

        dashboardService = new DashboardService(this.DbContext, (int)SessionManager.Instance["PlatformId"])
在进行会话到期检查之前。我无法将所有这些初始化移动到每个操作,因为它非常繁忙—我已经有很多操作方法

那么,当调用构造函数方法时,有没有办法检查会话超时?请帮忙。

我所做的是在应用程序的application\u acquisiteRequestState方法Global.asax中检查会话

像这样的

protected void Application_AcquireRequestState(object sender, EventArgs e)
{
     if (HttpContext.Current.Handler is IRequiresSessionState)
     {
         var usrobj = HttpContext.Current.Session["User"];
         if(usrobj == null)
         {
             Response.Redirect("~/Login/Index");
         }
     }
}

我不知道您是否想要类似的东西。

谢谢您的回复。是否有办法知道此应用程序中调用了哪个控制器和操作方法\u AcquisiteRequestState mathod?您不能在Global.asax中调用控制器或任何操作方法,你想用它实现什么?我不想从global.asax中调用controller或action方法,我只想检查调用的是哪个操作,因为我只想检查除了帐户控制器中的索引之外的其他操作的会话到期情况。我相信您可以通过以下方式检查调用的是哪个控制器/操作:string path=System.Web.HttpContext.Current.Request.path.ToString;