「ASP.NET Coreのセッションに文字列を入れる」の版間の差分
ナビゲーションに移動
検索に移動
imported>Administrator (ページの作成:「ASP .NET CoreのSessionクラスは標準でbyte[]を引数にとるSetメソッドしかない。 HttpContext.Session.Set(string key, byte[] value) ウェブ...」) |
imported>Administrator |
||
28行目: | 28行目: | ||
</source> | </source> | ||
− | [[category: ASP .NET Core]] | + | [[category: ASP.NET Core]] |
2018年10月2日 (火) 11:11時点における版
ASP .NET CoreのSessionクラスは標準でbyte[]を引数にとるSetメソッドしかない。
HttpContext.Session.Set(string key, byte[] value)
ウェブといえば文字列、PerlやRuby、PHPなどの文字列処理が手軽なプログラミング言語が勝ち続けてきた世界であり、 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();
}
}