asp.net core 2.2升级和HttpSessionState在类库中不可用 [英] asp.net core 2.2 upgrade and HttpSessionState is not available in class libraries

查看:739
本文介绍了asp.net core 2.2升级和HttpSessionState在类库中不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我继承了一个需要升级到.net core 2.2的项目时,我遇到了一些依赖项。

As I inherited a project that I need to upgrade to .net core 2.2 I'm having issues with several dependencies.

在这里我迷失了 HttpSessionState

private static string CollectionToHtmlTable(HttpSessionState collection)
    {
        // Converts HttpSessionState to NameValueCollection
        var nvc = new NameValueCollection();
        foreach (string item in collection)
        {
            nvc.Add(item, collection[item].ToString());
        }

        return CollectionToHtmlTable(nvc);
    }

关于如何访问> HttpSessionState ?

推荐答案

Microsoft .AspNetCore.Session 软件包(包含在 Microsoft.AspNetCore.App 元软件包中)提供了用于管理会话状态的中间件。
要启用会话中间件,启动必须包含:

The Microsoft.AspNetCore.Session package, which is included in the Microsoft.AspNetCore.App metapackage, provides middleware for managing session state. To enable the session middleware,Startup must contain:


  1. 任何IDistributedCache内存高速缓存。 IDistributedCache实现用作会话的后备存储。

  2. ConfigureServices中调用 AddSession code>。

  3. 配置中调用 UseSession

  1. Any of the IDistributedCache memory caches. The IDistributedCache implementation is used as a backing store for session.
  2. A call to AddSession in ConfigureServices.
  3. A call to UseSession in Configure.

代码:

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<CookiePolicyOptions>(options =>
        {
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });

        services.AddDistributedMemoryCache();

        services.AddSession(options =>
        {
            // Set a short timeout for easy testing.
            options.IdleTimeout = TimeSpan.FromSeconds(10);
            options.Cookie.HttpOnly = true;
        });

        services.AddMvc()
            .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Error");
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseCookiePolicy();
        app.UseSession();
        app.UseHttpContextItemsMiddleware();
        app.UseMvc();
    }
}

HttpContext.Session 在配置会话状态后可用。

HttpContext.Session is available after session state is configured.

HttpContext.Session 之前无法访问 UseSession 已被调用。

HttpContext.Session can't be accessed before UseSession has been called.

InvalidOperationException 异常在以下情况发生在 UseMvc 之后调用 UseSession

InvalidOperationException exception occurs when UseSession is invoked after UseMvc.

可以使用 HttpContext.Session 从Razor Pages PageModel类或MVC Controller类访问会话状态。此属性是 ISession 的实现。

Session state is accessed from a Razor Pages PageModel class or MVC Controller class with HttpContext.Session. This property is an ISession implementation.

ISession 实现提供了几种扩展方法来设置和检索整数和字符串值。扩展方法位于 Microsoft.AspNetCore.Http 命名空间中(添加一个

The ISession implementation provides several extension methods to set and retreive integer and string values. The extension methods are in the Microsoft.AspNetCore.Http namespace (add a

using Microsoft.AspNetCore.Http;

声明以访问扩展方法)
当项目引用 Microsoft.AspNetCore.Http.Extensions 包时。

statement to gain access to the extension methods) when the Microsoft.AspNetCore.Http.Extensions package is referenced by the project.

访问Httpcontext在类库中:

HttpContext 在控制器中可用,但要访问其他类,您需要在您的课程中插入 IHttpContextAccessor
要访问会话,请使用以下代码:-

HttpContext is available in controllers but to access it other classes you need to inject IHttpContextAccessor in your class. To access Session use below code:-

var sessionValue =_context.HttpContext.Session.GetString("KeyName");

更多详细信息,请访问:
https://docs.microsoft.com/zh-CN/aspnet/core/基本原理/app-state?view=aspnetcore-2.2

More details can be found at: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/app-state?view=aspnetcore-2.2

这篇关于asp.net core 2.2升级和HttpSessionState在类库中不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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