ASP.NETのセッションの型制御

CodeProjectの記事より

1.型の管理用ファサードクラス

public static class MySession
{
    private const string _myDate = "MyDate";
    public static DateTime MyDate
    {
        get 
        {
            if (HttpContext.Current.Session[_myDate] == null)
                return DateTime.MinValue;
            else
                return (DateTime)HttpContext.Current.Session[_myDate];
        }

        set
        {
            HttpContext.Current.Session[_myDate] = value;
        }
    }
}

2.セッションの設定/利用

MySession.MyDate = DateTime.Today.AddDays(-1);
DateTime myDate = MySession.MyDate;


ちなみに、WCSFでは、SessionStateKey属性でセッションを型指定できたと思います。

//「id」というキー名のint型のセッションを宣言
[SessionStateKey("id")]
public StateValue<int> _showId;

_showId.Value = 1; 
int id = _showId.Value;