2023开发学习内容集锦
01、HttpContext 上下文
.NET Core技术研究-HttpContext访问的正确姿势
应用场景
-
在Controller层访问HttpContext
-
在中间件中使用HttpContext
-
在数据访问层使用HttpContext
-
在后台线程中访问HttpContext
-
在Razor页面模型中访问HttpContext
-
在Razor视图中访问HttpContext
02、Session使用
- 1.注册服务:builder.Services.AddSession();
- 2.使用session:app.UseSession();
- 3.开始使用:
//写入
HttpContext.Session.SetString("tenantcode", model.tenantcode??"");
//获取
_httpContextAccessor?.HttpContext?.Session.GetString("tenantcode");
03、配置文件读取
使用方式:AppSettingsHelper.GetContent("ConnectionStrings", "SqlConnection")
查看全部代码:AppSettingsHelper
public class AppSettingsHelper
{
static IConfiguration Configuration { get; set; }
public AppSettingsHelper(string contentPath)
{
string Path = "appsettings.json";
Configuration = new ConfigurationBuilder().SetBasePath(contentPath).Add(new JsonConfigurationSource { Path = Path, Optional = false, ReloadOnChange = true }).Build();
}
/// <summary>
/// 封装要操作的字符
/// AppSettingsHelper.GetContent(new string[] { "JwtConfig", "SecretKey" });
/// </summary>
/// <param name="sections">节点配置</param>
/// <returns></returns>
public static string GetContent(params string[] sections)
{
try
{
if (sections.Any())
{
return Configuration[string.Join(":", sections)];
}
}
catch (Exception) { }
return "";
}
}

浙公网安备 33010602011771号