我在让 session 在我的 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();
的情况下运行应用程序,我会在使用 session 时收到以下错误:
An exception of type 'System.InvalidOperationException' occurred in Microsoft.AspNet.Http.dll but was not handled in user code
如有任何关于如何解决此问题的建议,我们将不胜感激:-)
最佳答案
你应该使用:
app.UseSession();
它位于 Microsoft.AspNet.Builder
命名空间中,因此您不需要 Session
命名空间。
关于c# - 在 ASP.NET 5 (MVC 6) 中使用 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32250659/