メインメニューを開く

差分

ASP.NET Coreのセッションに文字列を入れる

1,266 バイト追加, 2017年11月17日 (金) 08:25
ページの作成:「ASP .NET CoreのSessionクラスは標準でbyte[]を引数にとるSetメソッドしかない。 HttpContext.Session.Set(string key, byte[] value) ウェブ...」
[[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)

<source>
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();
}
}
</source>

[[category: ASP .NET Core]]
匿名利用者