Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.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# 会话值为空应用程序代码cs文件_C#_Asp.net_Session - Fatal编程技术网

C# 会话值为空应用程序代码cs文件

C# 会话值为空应用程序代码cs文件,c#,asp.net,session,C#,Asp.net,Session,我在app codesession.cs文件中使用了以下代码。当浏览器中的每个页面都被点击时,它最初会加载到httpmodule.cs中。我发现httpcontext.current.session值为null if (HttpContext.Current != null && HttpContext.Current.Session != null) { try { if (Http

我在app code
session.cs
文件中使用了以下代码。当浏览器中的每个页面都被点击时,它最初会加载到httpmodule.cs中。我发现
httpcontext.current.session
值为
null

if (HttpContext.Current != null && HttpContext.Current.Session != null)
        {
            try
            {
                if (HttpContext.Current.Session.Keys.Count > 0)
                {

                }
                else
                {
                    HttpContext.Current.Response.Cookies["ecm"].Expires = DateTime.Now;
                    HttpContext.Current.Response.Cookies["ecmSecure"].Expires = DateTime.Now;


                }
            }
            catch (Exception ex)
            {

            }
        }
web.config会话设置:

<sessionState mode="InProc" 
              stateConnectionString="tcpip=127.0.0.1:42424" 
              sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes" 
              cookieless="false" 
              timeout="5" />

一种可能的方法是将其包装在属性中,并像字符串一样访问它:-

string SessionVariable
{
   get{
      return Session("SessionVariable") ?? String.Empty;
   }
   set{
      Session("SessionVariable") = value;
   }
}
然后使用类似于:-

if( String.IsNullOrEmpty( SessionVariable) )
{
   // do something
}

谢谢朋友们的回复。我找到了答案。会话值由http模块生成。在访问会话值后,仅调用结束请求。下面是我使用的代码

public void Init(HttpApplication context)
{
    context.BeginRequest += new EventHandler(context_BeginRequest);
    context.EndRequest += new EventHandler(context_EndRequest);
    context.AcquireRequestState += new EventHandler(SessionClear);
}

public void SessionClear(object sender, EventArgs e)
{
    try
    {
        if (HttpContext.Current != null && HttpContext.Current.Session != null)
        {
            if (!(HttpContext.Current.Session.Keys.Count > 0))
            {

            }

        }
    }
    catch (Exception ex)
    {
    }
}

可能会有帮助的对不起,琼斯。这不是完全的“最初加载”的确切含义是什么?那是webforms还是MVC asp.net应用程序?如何在web.config中声明会话?最初加载了httpmodule.cs,然后我们访问那里的会话。所以最初的意思是在开始请求httpmodule?对不起,rahul。我没有使用web服务。我使用app code目录中的seesion.cs文件。那么iRequiresessionState呢?这是不受支持的…无论如何