webapi幂等
HttpHelper
打开HttpHelper,查看使用Http请求的方法
XMLHelper
动态获取属性值
csharp
PropertyValue<DemoClass> value=new PropertyValue<DeomClass>(instance);
string val=value.Get("Name");
枚举帮助
csharp
enum usergrade
{
[Description("vip1等级")]
vip1=1,
[Description("vip2")]
vip2=2
}
//获取枚举类型中的描述文字
usergrade.vip1.ToDescripton(); // 结果vip1等级
//根据值获取枚举类型
EnumHelper.GetEnumDesc<usergrade>(1); //结果 vip1
加解密
SecureHelper.AESEncrypt("要加密的字符串", "加密使用的key") SecureHelper.AESDecrypt("要解密的字符串","解密使用的key")
一个接口多个实现
接口继承IServiceBase
接口的实现,指定InstanceName
调用时,注入 IEnumerable<接口> services
csharp
var instance=services.FirstOfDefault(o=>o.InstanceName=="x");
Id生成器
csharp
services.AddSingleton<IdGenerate>(new IdGenerate(0,0,0));
注入IdGenerate id id.NewId()
获取IP
csharp
app.UseForwardedHeaders(new ForwardedHeadersOptions
{ ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto });
var ip = HttpContext.Connection.RemoteIpAddress;//ip地址
var port = HttpContext.Connection.RemotePort;//端口
获取ip需要代理配合,以nginx为例 nginx配置
location /
{
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header True-Client-IP $remote_addr;
proxy_pass http://127.0.0.1:51899;
}
csharp
var headers = httpContextAccessor?.HttpContext?.Request.Headers;
if (headers != null)
{
if (headers.ContainsKey("X-Forwarded-For"))
{
Ip = headers["X-Forwarded-For"].ToString();
// 处理获取到的 X-Forwarded-For 值以获取真实 IP
}
else if (headers.ContainsKey("X-Real-IP"))
{
Ip = headers["X-Real-IP"].ToString();
// 处理获取到的 True-Client-IP 值以获取真实 IP
}
}
JSON
csharp
var obj = JsonObject.Parse("{name:\"aa\",age:1}");
obj["name"].AsValue().ToString();
//解决汉字乱码
var option = new JsonSerializerOptions
{
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.Create(UnicodeRanges.All)
};
var s=JsonSerializer.Serialize(json,option);
参考微软文档