我有一个使用Session的ASP.Net vNext项目.但是我在尝试获取/设置会话中的值时收到此错误.
Microsoft.AspNet.Http.Core.dll中出现"System.InvalidOperationException"类型的异常,但未在用户代码中处理
附加信息:尚未为此应用程序或请求配置会话.
这是我的控制器方法:
[AllowAnonymous]
[HttpGet("/admin")]
public IActionResult Index()
{
if (Context.Session.GetString("UserName") == null) // error thrown here
{
return RedirectToAction("Login");
}
return View();
}
Run Code Online (Sandbox Code Playgroud)
我已经"Microsoft.AspNet.Session": "1.0.0-beta3"在我的project.json文件中添加了KVM包,并将我的应用程序配置为通过我的方式使用会话Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
// code removed for brevity
services.AddCachingServices();
services.AddSessionServices();
}
public void Configure(IApplicationBuilder app)
{
app.UseMvc();
app.UseInMemorySession(configure: s => s.IdleTimeout = TimeSpan.FromMinutes(30));
}
Run Code Online (Sandbox Code Playgroud)
我查看了Github上的vNext文档,但它没有提供有关ASP.Net会话的大量信息.我究竟做错了什么?