| | |
| | | using Microsoft.AspNetCore.Http; |
| | | using System.Dynamic; |
| | | using System.Dynamic; |
| | | |
| | | namespace NewPdaSqlServer.entity.Base; |
| | | |
| | | public class RequestInfo : DynamicObject |
| | | { |
| | | private readonly ExpandoObject _data = new(); |
| | | private readonly IHttpContextAccessor _httpContextAccessor; |
| | | private readonly ExpandoObject _data = new ExpandoObject(); |
| | | |
| | | public RequestInfo(IHttpContextAccessor httpContextAccessor) |
| | | { |
| | |
| | | |
| | | public override bool TryGetMember(GetMemberBinder binder, out object result) |
| | | { |
| | | string name = binder.Name; |
| | | return ((IDictionary<string, object>)_data).TryGetValue(name, out result); |
| | | var name = binder.Name; |
| | | return ((IDictionary<string, object>)_data).TryGetValue(name, |
| | | out result); |
| | | } |
| | | |
| | | public override bool TrySetMember(SetMemberBinder binder, object value) |
| | |
| | | public string GetHeaderValue(string key) |
| | | { |
| | | var httpContext = _httpContextAccessor.HttpContext; |
| | | if (httpContext?.Request.Headers.TryGetValue(key, out var value) == true) |
| | | { |
| | | return value.ToString(); |
| | | } |
| | | if (httpContext?.Request.Headers.TryGetValue(key, out var value) == |
| | | true) return value.ToString(); |
| | | return null; |
| | | } |
| | | |
| | |
| | | public void ValidateRequired(params string[] requiredFields) |
| | | { |
| | | foreach (var field in requiredFields) |
| | | { |
| | | if (!((IDictionary<string, object>)_data).ContainsKey(field) || |
| | | ((IDictionary<string, object>)_data)[field] == null) |
| | | { |
| | | throw new ArgumentException($"请求头中缺少必需的 {field} 参数"); |
| | | } |
| | | } |
| | | } |
| | | } |