「ASP.NET Coreのセッションに文字列を入れる」の版間の差分

提供: MonoBook
ナビゲーションに移動 検索に移動
imported>Administrator
(ページの作成:「ASP .NET CoreのSessionクラスは標準でbyte[]を引数にとるSetメソッドしかない。 HttpContext.Session.Set(string key, byte[] value) ウェブ...」)
 
 
(他の1人の利用者による、間の4版が非表示)
9行目: 9行目:
 
   HttpContext.Session.SetString(string key, string value)
 
   HttpContext.Session.SetString(string key, string value)
  
<source>
+
<source lang="csharp">
 
     using System;
 
     using System;
 
     using System.Collections.Generic;
 
     using System.Collections.Generic;
28行目: 28行目:
 
</source>
 
</source>
  
[[category: ASP .NET Core]]
+
[[category: ASP.NET Core]]

2024年3月7日 (木) 01:40時点における最新版

ASP .NET CoreのSessionクラスは標準でbyte[]を引数にとるSetメソッドしかない。

HttpContext.Session.Set(string key, byte[] value)

ウェブといえば文字列PerlRubyPHPなどの文字列処理が手軽なプログラミング言語が勝ち続けてきた世界であり、 ASP .NET Coreセッションにも文字列を簡単に突っ込みたいわけだ。 ググったらMicrosoft.AspNetCore.Http名前空間に「Session.SetString拡張メソッド」というズバリなものがいるらしい。Microsoft.AspNetCore.Session名前空間でないのかよ。わかりにくいな。

 HttpContext.Session.SetString(string key, string value)
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.AspNetCore.Http;

    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            // Session.SetString拡張メソッドは「Microsoft.AspNetCore.Http」にいる。
            HttpContext.Session.SetString("name", "monobook");
            return View();
        }
    }