我在我的MVC 6项目中遇到了会话(sessions)无法正常工作的问题。
我读过一篇很棒的文章,但是我不能像描述的那样在我的Startup.cs
中使app.UseInMemorySession();
正常工作。
我在project.json
文件的依赖项部分添加了"Microsoft.AspNet.Session": "1.0.0-beta6"
并修改了我的Startup.cs
如下所示:
我的配置部分看起来像这样:
public void ConfigureServices(IServiceCollection services)
{
// Add MVC services to the services container.
services.AddMvc();
services.AddSession();
services.AddCaching();
}
我的配置部分看起来像这样:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.MinimumLevel = LogLevel.Information;
loggerFactory.AddConsole();
// Configure the HTTP request pipeline.
// Add the following to the request pipeline only in development environment.
if (env.IsDevelopment())
{
app.UseBrowserLink();
app.UseErrorPage();
}
else
{
// Add Error handling middleware which catches all application specific errors and
// send the request to the following path or controller action.
app.UseErrorHandler("/Home/Error");
}
// Add static files to the request pipeline.
app.UseStaticFiles();
// Configure sessions:
//app.UseInMemorySession();
// Add MVC to the request pipeline.
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
请注意,
app.UseInMemorySession();
已被注释掉,因为启用它会导致以下错误:'IApplicationBuilder' does not contain a definition for 'UseInMemorySession' and no extension method 'UseInMemorySession' accepting a first argument of type 'IApplicationBuilder' could be found (are you missing a using directive or an assembly reference?)
如果我尝试在没有使用
app.UseInMemorySession();
的情况下运行应用程序,则在使用会话时会出现以下错误:An exception of type 'System.InvalidOperationException' occurred in Microsoft.AspNet.Http.dll but was not handled in user code
欢迎提供解决此问题的任何建议 :-)
Microsoft.AspNet.Builder.SessionMiddlewareExtensions
中。你是否添加了Microsoft.AspNet.Builder
的using语句?(它可能是默认存在的,但最好确认一下) - mason