问题描述
我无法让会话在我的MVC 6项目中正常工作.
I'm having trouble getting sessions to work in my MVC 6 project.
我阅读了关于此的出色文章 ,但是我无法按所述在Startup.cs
中使app.UseInMemorySession();
正常工作.
I read a great article about this , but I cant get app.UseInMemorySession();
to work in my Startup.cs
as described.
我在project.json
的依赖项部分中添加了"Microsoft.AspNet.Session": "1.0.0-beta6"
,并对我的Startup.cs
进行了如下修改:
I added "Microsoft.AspNet.Session": "1.0.0-beta6"
to the dependencies part of my project.json
and modified my Startup.cs
as follows:
我的配置"部分如下所示:
My Configuration part looks like this:
public void ConfigureServices(IServiceCollection services)
{
// Add MVC services to the services container.
services.AddMvc();
services.AddSession();
services.AddCaching();
}
我的配置"部分如下所示:
My Configure part looks like this:
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();
已被注释掉,因为如果启用它,则会出现以下错误:
Note that app.UseInMemorySession();
is commented out, since if I enable it, I get the following error:
'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();
的情况下运行该应用程序,则在使用会话时出现以下错误:
If I try to run the application without app.UseInMemorySession();
I get the following error when using sessions:
An exception of type 'System.InvalidOperationException' occurred in Microsoft.AspNet.Http.dll but was not handled in user code
对于如何解决此问题的任何建议,我们都会感激不尽:-)
Any advice on how to solve this problem is appreciated :-)
推荐答案
您应使用:
app.UseSession();
它位于Microsoft.AspNet.Builder
命名空间中,因此您不需要Session
命名空间.
It lives in the Microsoft.AspNet.Builder
namespace, so you should not need the Session
namespace.
这篇关于在ASP.NET 5(MVC 6)中使用会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!