none
Global.asax中Application_AcquireRequestState对默认文档(default.aspx)生效时候session不支持的问题 RRS feed

  • 问题


  • 在IIS中default.aspx已经是默认文档。并且能够成功访问http://www.域名.com/和http://www.域名.com/default.aspx。

    但是我在Global.asax中Application_AcquireRequestState中判断session相关内容的时候,只能支持http://www.域名.com/default.aspx。在输入 http://www.域名.com/会提示【会话状态在此上下文中不可用。】。

    请问如何操作才能改好?

    2018年6月25日 13:44

答案

  • The session gets loaded during Application_AcquireRequestState. Your safe bet is to build Application_PreRequestHandlerExecute and access it there.


    Update: Not every request has a session state. You need to also check for null: if (System.Web.HttpContext.Current.Session != null).

    var session = HttpContext.Current.Session;
    
    if(session != null) {
        /* ... do stuff ... */
    }
     public class MvcApplication : System.Web.HttpApplication
        {
            protected void Application_Start()
            {
                 /* ... */
            }
    
            protected void Application_PreRequestHandlerExecute(Object sender, EventArgs e)
            {
                if (HttpContext.Current.Session != null && HttpContext.Current.Session["isLogged"] != null && (bool)HttpContext.Current.Session["isLogged"])
                {
                    HttpContext.Current.User = (LoginModel)HttpContext.Current.Session["LoginModel"];
                }
            }
        }


    专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms

    2018年6月26日 0:08