c# - 在 ASP.NET 5 (MVC 6) 中使用 session

标签 c# session asp.net-core asp.net-core-mvc

我在让 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

InvalidOperationException was unhandled by user code

如有任何关于如何解决此问题的建议,我们将不胜感激:-)

最佳答案

你应该使用:

app.UseSession();

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

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

关于c# - 在 ASP.NET 5 (MVC 6) 中使用 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32250659/

相关文章:

r - 使用 data.table 中的 fread() 会导致 R session 中止

linux - 如何在 CentOS 上不注销/登录的情况下刷新 session ?

asp.net-core - MailKit.Security.SslHandshakeException : The host name did not match the name given in the server's SSL certificate. asp.net 核心 5,nginx

c# - 使用 Razor 页面返回 XML?

debugging - VS 2015 Update 2 - 调试时变量不存在,为什么?

c# - SlowCheetah 没有为 EntityFramework 迁移进行转换

c# - 在 KeyValuePair 键上相交列表?

c# - 是否可以在 WPF 中实现工具栏并绘制控件?

c# - ASP :NET MVC: How can I render two styles in one page without any overlaps of CSS classes?

java - 基于原始身份验证在 session 超时后动态重定向用户