Using sessions in ASP.NET 5 (MVC 6)

半城伤御伤魂 提交于 2019-12-10 17:18:46

问题


I'm having trouble getting sessions to work in my MVC 6 project.

I read a great article about this , but I cant get app.UseInMemorySession(); to work in my Startup.cs as described.

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?}");
        });
    }

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?)

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 :-)


回答1:


You should use:

app.UseSession();

It lives in the Microsoft.AspNet.Builder namespace, so you should not need the Session namespace.

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




回答2:


Seems I needed two changes.

First I needed to add using Microsoft.AspNet.Session;, and I had to change: app.UseInMemorySession(); to app.UseSession(); in the Configure part of Startup.cs.

At least when I do this, it starts working without throwing exceptions.




回答3:


Adding a note for anyone trying to use rc2, you get the following exception.

ILoggerFactory' does not contain a definition for 'MinimumLevel' and no extension method 'MinimumLevel' accepting a first argument of type 'ILoggerFactory' could be found (are you missing a using directive or an assembly reference?)

The MinimumLevel has been removed as per the following breaking change: https://github.com/aspnet/Announcements/issues/122

 loggerFactory.MinimumLevel = LogLevel.Information;

That line just needs removing from your code (and OP's code) for rc2+.



来源:https://stackoverflow.com/questions/32250659/using-sessions-in-asp-net-5-mvc-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!