会话包装器asp.net [英] session wrapper asp.net

查看:94
本文介绍了会话包装器asp.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在使用会话助手类来管理会话,因为我有很多会话变量,并且在各处都使用硬编码的会话很麻烦,下面是我使用过的类

Hi all,

i am using session helper class to manage session,because i had many session variables and it was messy to use hardcoded session everywhere,below is the class i have used

public static class Sessionhelper
{
    private static T GetFromSession<T>(string key)
    {
        object obj =HttpContext.Current.Session[key];
        if (obj == null)
        {
            return default(T);
        }
        return (T)obj;
    }

    private static void SetInSession<T>(string key, T value)
    {
        if (value == null)
        {
            HttpContext.Current.Session.Remove(key);
        }
        else
        {
            HttpContext.Current.Session[key] = value;
        }
    }



以及访问它们的属性



and the properties to access them

public static string CustomerName
   {
       get
       {
           if (string.IsNullOrEmpty(GetFromSession<string>("Customerselected")))
           {
               HttpContext.Current.Response.Redirect("~/My_App/MYUI/Home.aspx", false);
               return GetFromSession<string>("Customerselected");
           }
           else
           {
               return GetFromSession<string>("Customerselected");
           }
       }
       set { SetInSession<string>("Customerselected", value); }
   }

   public static string fid
   {
       get { return GetFromSession<string>("fid"); }
       set { SetInSession<string>("fid", value); }
   }



1)如果customerselected为null,我将重定向到主页,线程安全吗?正确的方法吗?
2)我可以使用
将值保存到会话中



1)if the customerselected is null i am redirecting to home page, is it thread safe to do,and correct way?
2)i can save value to session using

Sessionhelper.UserId = userid.ToString();



但是我得到的空引用异常未由用户代码处理(对象引用未设置为对象的实例)



but i get null reference exception was unhandled by user code( object reference was not set to instance of an object)

 private static T GetFromSession<T>(string key)
    {
        object obj =HttpContext.Current.Session[key];//here i get that exception
..
}



尝试获取会话值时


任何帮助将不胜感激.



when try to get session value


any help would be greatly appreciated.

推荐答案

可以检查您的会话.它会过期吗?因为即使未找到会话密钥,您也不会得到null异常错误.
Could you check your session. does it get expire? Because even if Session key is not found, you s''d not get null exception error.


您可以执行以下操作,首先检查密钥是否存在...

You could possibly do something like this, to check the key exists first...

public static T Get<T>(string key)
{
    if (Session[key] == null)
        return default(T);
    else
        return (T)Session[key];
}



看看这个线程,它显示了与您类似的技术

http://stackoverflow.com/questions/234973/what-is-the-best-way-to-determine-a-session-variable-is-null-or-empty-in-c [
例如



Have a look at this thread, which shows a similar technique to you

http://stackoverflow.com/questions/234973/what-is-the-best-way-to-determine-a-session-variable-is-null-or-empty-in-c[^]

Also, don''t mix your ''Session state management'' and your ''application'' logic, i.e. you are redirecting from the session state manager. Have whatever accesses that, manange the redirect

e.g.

if (string.IsNullOrEmpty(Sessionhelper.UserName))
    HttpContext.Current.Response.Redirect("~/My_App/MYUI/Home.aspx", false);


这篇关于会话包装器asp.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆