我有一个使用Session的ASP.Net vNext项目。但是在尝试获取/设置会话中的值时出现此错误。无法在ASP.Net vNext项目中使用会话
“System.InvalidOperationException”类型的异常出现在Microsoft.AspNet.Http.Core.dll但在用户代码中没有处理
其他信息:会话尚未配置用于该应用或请求。
这里是我的控制器的方法:
[AllowAnonymous]
[HttpGet("/admin")]
public IActionResult Index()
{
if (Context.Session.GetString("UserName") == null) // error thrown here
{
return RedirectToAction("Login");
}
return View();
}
我在project.json
文件添加的KVM包"Microsoft.AspNet.Session": "1.0.0-beta3"
以及并已通过我的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));
}
我已经看过Github上的vNext文档,但它没有提供关于ASP.Net会话的很多信息。我究竟做错了什么?
是的,这是有帮助的。我只是移动'''app.UseSession();'''在app.UseMvc();之前''' – feradz