2011-03-05 111 views
3

當我們訪問Session["key"].ToString()時,它會在會話過期時發生異常&我們嘗試訪問會話變量。所以我想對object類創建擴展方法,這樣我可以在編碼寫爲Session["key"].getString()讓每一個我沒有時間做Convert.ToString(session["key"])Session [「key」]的字符串擴展方法

任何其他的解決方案也是明顯的。

回答

4

只需使用空合併運算符:

string value = (session["key"] ?? String.Empty).ToString(); 

更新
如果你必須要做到這一點(分機或其他方式)的方法,我會做這樣的事情:

public static string GetValue(this HttpSessionState session, string key) 
{ 
    // TODO: Insert appropriate error checking here. 

    return (session[key] ?? String.Empty).ToString(); 
} 

我甚至可以爲其他可能的類型制定通用模式,其中GetValue調用需要使用選擇器,然後使用lambdas:

public static T GetValue<T>(this HttpSessionState session, string key, Func<object, T> valueSelector) 
{ 
    return valueSelector(session[key]); 
} 

public static string GetStringValue(this HttpSessionState session, string key) 
{ 
    return session.GetValue(key, x => (x ?? String.Empty).ToString()); 
} 

然後,您可以使用如下:

string value = session.GetStringValue("key"); 
+0

這將工作,但擴展方法將是可取的,因爲編寫全部代碼可能是麻煩和重複。 – 2011-03-05 17:54:55

+1

yates你可以提供關於如何使用上述通用擴展方法的簡短信息。 – 2011-03-05 20:07:07

5
public static class ObjectExtensions 
{ 
    public static string GetString(this object o) 
    { 
     if (o == null) 
     { 
      return string.Empty; 
     } 
     return Convert.ToString(o); 
    } 
} 

然後:

string value = Session["key"].GetString(); 

或檢查這一項:

public static class SessionExtensions 
{ 
    public static string GetString(this HttpSessionStateBase session, string key) 
    { 
     if (session == null) 
     { 
      return string.Empty; 
     } 
     var value = session[key]; 
     if (value == null) 
     { 
      return string.Empty; 
     } 
     return Convert.ToString(value); 
    } 
} 

然後:

string value = Session.GetString("key"); 
0

或許這能夠BA的方法嗎?

object oKey = session["key"] ?? String.Empty; 
string sKey = (string)oKey; 

string sKey = session["key"] == null ? String.Empty : (string)session["key"] 
0

我也建議使用該特性。

protected YourType PropertyName 
{ 
    get 
    { 
    if(Session["Sessionname"] != null) 
    { 
     return Session["Sessionname"] as YourType; 
    } 
    YourType newItem = new YourType(); 
    // set vars 
    Session["Sessionname"] = newItem; 
    return newItem; 
    } 
    set 
    { 
    Session["Sessionname"] = value; 
    } 
} 

正如你所看到的,我選擇了protected作爲訪問修飾符。如果你願意,你可以把它放在一個public class,使財產static。您可以使用System.Web.UI.Page派生的類。現在

public class MyCustomBaseClass : System.Web.UI.Page 
{ 
    protected YourType PropertyName 
    { 
    // get and set like above 
    } 
} 

你可以從System.Web.UI.Page取代繼承你的內容頁面,以MyCustomBaseClass,可以方便地調用this.PropertyName

+0

它很可能會給選項來存儲對象的對象或列表或指會話中的任何數據類型? – 2011-03-05 19:59:49

+0

@Harsh:每個可序列化的數據類型。我的回答沒有用嗎? – citronas 2011-03-06 08:56:12

+0

你的回答也很有用,但@Jeff Yates的回答更加簡單和簡短,這就是我接受他的答案的原因。 – 2011-03-07 05:18:19

0
public static class ObjectExtensions 
{ 
    public static string SafelyToString(this object o) 
    { 
    return o != null ? o.ToString() : string.Empty; 
    } 
} 

這將使Session["key"].SafelyToString()

你不會然而能夠在會話變量一個空字符串和過期之間的辨別會話。

0

這裏有一個簡單的方法來訪問會話中擴展方法

var loggedUser = (User)System.Web.HttpContext.Current.Session["User"];