Shared/Kernelx.AspNetCore/Kernelx/Contexts/HttpRequestParameter.cs

(开头部分) 4KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

using Microsoft.AspNetCore.Http; 
using System;
using System.Collections.Generic; 
using System.IO;
using System.Linq;
using System.Reflection; 

namespace Kernelx.Contexts
{
    internal class HttpRequestParameter : IRequestParameter
    { 
        private readonly HttpRequest _httpRequest;
        public HttpRequestParameter(HttpRequest httpRequest)
        {
            _httpRequest = httpRequest;
        }

        private Dictionary<string, object> __data;


        private static readonly List<string> _specialKeys = new() { "area", "controller", "action" };

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE1006:命名样式", Justification = "<挂起>")]
        private Dictionary<string, object> _GetData()
        {
            if (__data == null)
            {
                lock (this)
                    if (__data == null)
                    {
                        __data = new Dictionary<string, object>();
                        foreach (var q in _httpRequest.Query)
                            __data[q.Key] = q.Value.Count>1?q.Value.ToArray():q.Value.ToString();

                        if (_httpRequest.HasFormContentType)
                        {
                            foreach (var f in _httpRequest.Form)
                                __data[f.Key] = f.Value.Count > 1 ? f.Value.ToArray() : f.Value.ToString();
                        }

                        var rvp = _httpRequest.GetType().GetProperty("RouteValues", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.FlattenHierarchy);
                        if (rvp != null)
                        {
                            var routevalues = rvp.GetValue(_httpRequest);
                            var kp = routevalues.GetType().GetProperty("Keys");
                            var vp = routevalues.GetType().GetProperty("Values");
                            var keys = (kp.GetValue(routevalues) as IEnumerable<string>).ToList();
                            var values = (vp.GetValue(routevalues) as IEnumerable<object>).ToList();
                            for (var i = 0; i < keys.Count; i++)
                            {
                                var key = keys[i];
                                if (_specialKeys.Contains(key))
                                    continue;
                                __data[key] = values[i]?.ToString();
                            }
                        }

                        if (_httpRequest.Body.CanRead && (_httpRequest.ContentType ?? string.Empty).Contains("json"))
                        {
                            var serializer = ContextWorkload.Current.GetJsonSerializer();
后面还有 47 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明

AI 解读

登录后可用,每次 10 积分,解读结果公开显示在下面。

还没有人解读过这个文件。