ASP.NET COREでSESSIONを使用
1908 ワード
からhttps://www.cnblogs.com/liuxiaoji/p/6860104.html 炒めたのは、ここで自分のブログに記入して、記録をして、後で炒めても自分のブログのコードを炒めて、ASP.NET COREでSESSIONを使用する手順は以下の通りです. NUGETパッケージはMicrosoftを参照する.AspNetCore.Session(注:私がmacの下でvs for macで作ったとき、NUGETは全然検索できません.FQが必要です.自分で検索して、ブログ園のNUGETソースに加入すればいいです.そして、すぐに:https://nuget.cnblogs.com/v3/index.json) Startup.csの対応する方法はいくつかのコードを加えます: 以下はコントローラでSESSIONを使用するコードです.まずそのネーミングスペースを参照してください. using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using candel.Models; using Microsoft.AspNetCore.Http;//これを引用するのを忘れないでください.
namespace candel.Controllers{public class HomeController:Controller{public IActionResult Index(){ViewBag.msg=「こんにちは、牛の腹、ハハハ!!」;HttpContext.Session.SetString("username","niunan");//SESSION return View();
}
public void ConfigureServices(IServiceCollection services)
{
// session
services.AddDistributedMemoryCache();
services.AddSession();
services.AddMvc();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseSession(); // session
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
namespace candel.Controllers{public class HomeController:Controller{public IActionResult Index(){ViewBag.msg=「こんにちは、牛の腹、ハハハ!!」;HttpContext.Session.SetString("username","niunan");//SESSION return View();
public IActionResult About(){
string username = HttpContext.Session.GetString("username"); // SESSION
ViewBag.username = username;
return View();
}
}
}