【发布时间】:2015-11-21 21:52:49
【问题描述】:
我无法让会话在我的 MVC 6 项目中工作。
我读到了 great article about this ,但我无法让 app.UseInMemorySession(); 像描述的那样在我的 Startup.cs 中工作。
我将"Microsoft.AspNet.Session": "1.0.0-beta6"添加到我的project.json的依赖项部分并修改了我的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
感谢任何有关如何解决此问题的建议 :-)
【问题讨论】:
-
您是否尝试过签出the source code 并直接引用它?您尝试使用的方法是
Microsoft.AspNet.Builder.SessionMiddlewareExtensions。您是否为Microsoft.AspNet.Builder添加了 using 语句? (它可能默认存在,但不妨确认)
标签: c# session asp.net-core asp.net-core-mvc