【问题标题】:Using sessions in ASP.NET 5 (MVC 6)在 ASP.NET 5 (MVC 6) 中使用会话
【发布时间】: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


【解决方案1】:

为尝试使用 rc2 的任何人添加注释,您会得到以下异常。

ILoggerFactory 不包含“MinimumLevel”的定义,并且找不到接受“ILoggerFactory”类型的第一个参数的扩展方法“MinimumLevel”(您是否缺少 using 指令或程序集引用?)

已根据以下重大更改删除了 MinimumLevel: https://github.com/aspnet/Announcements/issues/122

 loggerFactory.MinimumLevel = LogLevel.Information;

该行只需要从 rc2+ 的代码(和 OP 的代码)中删除。

【讨论】:

    【解决方案2】:

    你应该使用:

    app.UseSession();
    

    它位于 Microsoft.AspNet.Builder 命名空间中,因此您不需要 Session 命名空间。

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

    【讨论】:

    • 你说对了 - 我在发布类似答案时为时已晚 56 秒 :-)
    【解决方案3】:

    看来我需要做两处改变。

    首先我需要添加using Microsoft.AspNet.Session;,然后我必须更改: app.UseInMemorySession();app.UseSession();Startup.cs 的配置部分。

    至少当我这样做时,它开始工作而不会抛出异常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-04
      • 2020-12-13
      • 2016-03-24
      • 2016-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多