爆肝,重构框架,你懂得

This commit is contained in:
chenchun
2023-01-01 23:06:11 +08:00
parent dbe020dc94
commit b9384afd5d
276 changed files with 5205 additions and 3281 deletions

View File

@@ -0,0 +1,92 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Yi.Framework.WebCore.CommonExtend
{
/// <summary>
/// appsettings.json操作类
/// </summary>
public class Appsettings
{
static IConfiguration? Configuration { get; set; }
static string? contentPath { get; set; }
public Appsettings(string contentPath)
{
string Path = "appsettings.json";
//如果你把配置文件 是 根据环境变量来分开了,可以这样写
//Path = $"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json";
Configuration = new ConfigurationBuilder()
.SetBasePath(contentPath)
.Add(new JsonConfigurationSource { Path = Path, Optional = false, ReloadOnChange = true })//这样的话可以直接读目录里的json文件而不是 bin 文件夹下的,所以不用修改复制属性
.Build();
}
public Appsettings(IConfiguration configuration)
{
Configuration = configuration;
}
/// <summary>
/// 封装要操作的字符
/// </summary>
/// <param name="sections">节点配置</param>
/// <returns></returns>
public static string? app(params string[] sections)
{
try
{
if (sections.Any())
{
return Configuration?[string.Join(":", sections)];
}
}
catch (Exception) { }
return "";
}
public static bool appBool(params string[] sections)
{
return Bool(app(sections));
}
public static bool Bool(object? thisValue)
{
bool reval = false;
if (thisValue != null && thisValue != DBNull.Value && bool.TryParse(thisValue.ToString(), out reval))
{
return reval;
}
return reval;
}
/// <summary>
/// 递归获取配置信息数组
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="sections"></param>
/// <returns></returns>
public static T app<T>(params string[] sections)
{
T app = Activator.CreateInstance<T>();
// 引用 Microsoft.Extensions.Configuration.Binder 包
Configuration.Bind(string.Join(":", sections), app);
return app;
}
public static IConfiguration? appConfiguration(params string[] sections)
{
return Configuration?.GetSection(string.Join(":", sections));
}
}
}

View File

@@ -7,14 +7,14 @@ using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Model.Models;
using System.IdentityModel.Tokens.Jwt;
using System.IO;
using System.Text.RegularExpressions;
using UAParser;
using IPTools.Core;
using Yi.Framework.Model.RABC.Entitys;
namespace Yi.Framework.WebCore
namespace Yi.Framework.WebCore.CommonExtend
{
public static class HttpContextExtend
{
@@ -113,7 +113,7 @@ namespace Yi.Framework.WebCore
/// <param name="fileName"></param>
public static void FileInlineHandle(this HttpContext httpContext, string fileName)
{
string encodeFilename = System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.GetEncoding("UTF-8"));
string encodeFilename = System.Web.HttpUtility.UrlEncode(fileName, Encoding.GetEncoding("UTF-8"));
httpContext.Response.Headers.Add("Content-Disposition", "inline;filename=" + encodeFilename);
}
@@ -125,7 +125,7 @@ namespace Yi.Framework.WebCore
/// <param name="fileName"></param>
public static void FileAttachmentHandle(this HttpContext httpContext, string fileName)
{
string encodeFilename = System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.GetEncoding("UTF-8"));
string encodeFilename = System.Web.HttpUtility.UrlEncode(fileName, Encoding.GetEncoding("UTF-8"));
httpContext.Response.Headers.Add("Content-Disposition", "attachment;filename=" + encodeFilename);
}
@@ -167,7 +167,7 @@ namespace Yi.Framework.WebCore
}
else
{
param = context.Request.QueryString.Value is null?"": context.Request.QueryString.Value.ToString();
param = context.Request.QueryString.Value is null ? "" : context.Request.QueryString.Value.ToString();
}
return param;
}
@@ -179,7 +179,7 @@ namespace Yi.Framework.WebCore
/// <returns></returns>
public static ClientInfo GetClientInfo(this HttpContext context)
{
var str = GetUserAgent(context);
var str = context.GetUserAgent();
var uaParser = Parser.GetDefault();
ClientInfo c = uaParser.Parse(str);
return c;

View File

@@ -0,0 +1,26 @@
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using System;
using Ubiety.Dns.Core.Common;
namespace Yi.Framework.WebCore.CommonExtend
{
public static class ServiceLocator
{
public static IServiceProvider? Instance { get; set; }
//需要兼容不存在http请求的情况
public static bool GetHttp(out HttpContext? httpContext)
{
httpContext = null;
var httpContextAccessor = Instance?.GetService<IHttpContextAccessor>();
if (httpContextAccessor is null)
{
return false;
}
httpContext = httpContextAccessor.HttpContext;
return true;
}
}
}