Skip to content

Instantly share code, notes, and snippets.

@kuju63
Last active January 12, 2018 11:32
Show Gist options
  • Save kuju63/0cb2588f239995051364de25979c9106 to your computer and use it in GitHub Desktop.
Save kuju63/0cb2588f239995051364de25979c9106 to your computer and use it in GitHub Desktop.
ASP.NET MVCでのセッションへの格納・取得
using System.Web.Mvc;
namespace WebApplication1.Controllers
{
/// <summary>
/// セッションに格納するデータ
/// </summary>
[Serializable]
public class Foo
{
/// <summary>
/// セッションデータ
/// </summary>
public string Bar { get; set; }
}
public class SampleController : Controller
{
// GET: Sample
public ActionResult Index()
{
var sessionData = new Foo();
sessionData.Bar = "bar";
// セッションへの格納
Session["SESSION_KEY"] = sessionData;
return View();
}
public ActionResult Next()
{
// セッションの取得
var sessionData = Session["SESSION_KEY"] as Foo;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment