asp.net core 2.2 升级和 HttpSessionState 在类库中不可用 [英] asp.net core 2.2 upgrade and HttpSessionState is not available in class libraries
问题描述
当我继承了一个需要升级到 .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);
}
关于如何访问 .net 核心类库中的 HttpSessionState
有哪些好的代码示例?
What are some good code samples of how I can access HttpSessionState
in a .net core class library?
推荐答案
Microsoft.AspNetCore.Session
包,包含在 Microsoft.AspNetCore.App
元包中,提供管理会话状态的中间件.要启用会话中间件,Startup 必须包含:
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:
- 任何 IDistributedCache 内存缓存.IDistributedCache 实现用作会话的后备存储.
- 调用
ConfigureServices
中的AddSession
. - 在
Configure
中调用UseSession
.
- Any of the IDistributedCache memory caches. The IDistributedCache implementation is used as a backing store for session.
- A call to
AddSession
inConfigureServices
. - A call to
UseSession
inConfigure
.
代码:
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
.
会话状态是从 Razor Pages PageModel 类或带有 HttpContext.Session
的 MVC 控制器类访问的.此属性是一个 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/en-us/aspnet/core/fundamentals/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屋!