在ASP.NET 5(MVC 6)中使用会话 [英] Using sessions in ASP.NET 5 (MVC 6)

查看:88
本文介绍了在ASP.NET 5(MVC 6)中使用会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法让会话在我的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.

请参阅: https://github.com/aspnet/Session/blob/1.0.0-beta6/src/Microsoft.AspNet.Session/SessionMiddlewareExtensions.cs

这篇关于在ASP.NET 5(MVC 6)中使用会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆