You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

55 lines
1.4 KiB

using ce.autofac.extension;
using Microsoft.AspNetCore.Http;
namespace learun.util
{
/// <summary>
/// 版 本 EasyCode EC管理后台
/// Copyright (c) 2019-present EC管理有限公司
/// 创建人:tobin
/// 日 期:2019.09.11
/// 描 述:上下文上下文帮助类
/// </summary>
public static class ContextHelper
{
/// <summary>
/// 获取上下文值
/// </summary>
/// <param name="key">键</param>
/// <returns></returns>
public static object GetItem(string key)
{
var accessor = IocManager.Instance.GetService<IHttpContextAccessor>();
accessor.HttpContext.Items.TryGetValue(key, out object res);
return res;
}
/// <summary>
/// 设置上下文值
/// </summary>
/// <param name="key">键</param>
/// <param name="value">值</param>
public static void SetItem(string key, string value)
{
var accessor = IocManager.Instance.GetService<IHttpContextAccessor>();
if (accessor.HttpContext.Items.ContainsKey(key))
{
accessor.HttpContext.Items.Remove(key);
}
accessor.HttpContext.Items.Add(key, value);
}
/// <summary>
/// 移除上下文值
/// </summary>
/// <param name="key"></param>
public static void RemoveItem(string key)
{
var accessor = IocManager.Instance.GetService<IHttpContextAccessor>();
if (accessor.HttpContext.Items.ContainsKey(key))
{
accessor.HttpContext.Items.Remove(key);
}
}
}
}