新しいオブジェクトを何度も作成したくなかったので、すべての直接的なセッション対話を別のクラスに分けて静的にしました。ただし、並行性の問題やその他の不安定な驚きがないことを確認したいと思います。
コードは次のとおりです。
public static class HttpHelper
{
public static string Get(string key)
{
object value = HttpContext.Current.Request.QueryString[key];
return (value == null) ? null : value.ToString();
}
public static string Post(string key)
{
object value = HttpContext.Current.Request.Form[key];
return (value == null) ? null : value.ToString();
}
public static string Session(string key)
{
object value = HttpContext.Current.Session[key];
return (value == null) ? null : value.ToString();
}
public static void ClearSession(string key)
{
HttpContext.Current.Session[key] = null;
}
public static void StoreInSession(string key, object value)
{
HttpContext.Current.Session[key] = value;
}
}