Exemplo n.º 1
0
        /// <summary>
        /// 获取目标数据表单数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        protected T GetTargetData <T>() where T : EntityBase <T>
        {
            T data = default(T);

            if (FormData != null && FormData.ContainsKey(EntityBase <T> .PrimaryKeyName))
            {
                object primaryKeyValue = FormData[EntityBase <T> .PrimaryKeyName];

                data = EntityBase <T> .AutoFind(primaryKeyValue);
            }

            return(data);
        }
Exemplo n.º 2
0
        public T GetModel <T>(T model)
        {
            if (model == null)
            {
                model = GetModel <T>();
                return(model);
            }
            System.Reflection.PropertyInfo[] propertys = model.GetType().GetProperties();
            for (int j = 0; j < propertys.Length; j++)
            {
                if (propertys[j].Name == "WorkId")
                {
                    break;
                }
                if (FormData.ContainsKey(propertys[j].Name) == true)
                {
                    if (propertys[j].PropertyType.Equals(typeof(Int32)))
                    {
                        propertys[j].SetValue(model, Convert.ToInt32(FormData[propertys[j].Name].Trim() == "" ? "0" : FormData[propertys[j].Name]), null);
                    }
                    else if (propertys[j].PropertyType.Equals(typeof(Int64)))
                    {
                        propertys[j].SetValue(model, Convert.ToInt64(FormData[propertys[j].Name].Trim() == "" ? "0" : FormData[propertys[j].Name]), null);
                    }
                    else if (propertys[j].PropertyType.Equals(typeof(decimal)))
                    {
                        propertys[j].SetValue(model, Convert.ToDecimal(FormData[propertys[j].Name].Trim() == "" ? "0" : FormData[propertys[j].Name]), null);
                    }
                    else if (propertys[j].PropertyType.Equals(typeof(DateTime)))
                    {
                        propertys[j].SetValue(model, Convert.ToDateTime(FormData[propertys[j].Name].Trim() == "" ? DateTime.Now.ToString() : FormData[propertys[j].Name]), null);
                    }
                    else
                    {
                        propertys[j].SetValue(model, FormData[propertys[j].Name], null);
                    }
                }
            }

            return(model);
        }
Exemplo n.º 3
0
        private void ParseRequestFormDataParameters(string requestBody)
        {
            if (string.IsNullOrEmpty(requestBody) == false)
            {
                //TODO: Parse Multiple Parameters By Name
                var paramsPairs = requestBody
                                  .Split('&')
                                  .Select(plainQueryParameter => plainQueryParameter.Split('='))
                                  .ToList();

                foreach (var paramPair in paramsPairs)
                {
                    string key   = paramPair[0];
                    string value = paramPair[1];
                    if (FormData.ContainsKey(key) == false)
                    {
                        FormData.Add(key, new HashSet <string>());
                    }
                    FormData.Add(key, value);
                }
            }
        }
Exemplo n.º 4
0
        public HttpRequest(string requestString)
        {
            var lines = requestString.Split(new string[] { HttpConstants.NEW_LINE }, StringSplitOptions.None);

            var headerLine = lines[0];

            var headerLineParts = headerLine.Split();

            this.Method = (HttpMethod)Enum.Parse(typeof(HttpMethod), headerLineParts[0], true);
            this.Path   = headerLineParts[1];

            int  lineIndex   = 1;
            bool isInHeaders = true;

            var bodyBuilder = new StringBuilder();

            while (lineIndex < lines.Length)
            {
                var line = lines[lineIndex];

                lineIndex++;

                if (string.IsNullOrWhiteSpace(line))
                {
                    isInHeaders = false;

                    continue;
                }

                if (isInHeaders)
                {
                    this.Headers.Add(new Header(line));
                }
                else
                {
                    bodyBuilder.AppendLine(line);
                }
            }

            if (this.Headers.Any(h => h.Name == HttpConstants.REQUEST_COOKIE_HEADER))
            {
                var cookiesAsString = this.Headers.FirstOrDefault(h => h.Name == HttpConstants.REQUEST_COOKIE_HEADER).Value;

                var cookies = cookiesAsString.Split(new string[] { "; " }, StringSplitOptions.RemoveEmptyEntries);

                foreach (var cookie in cookies)
                {
                    this.Cookies.Add(new Cookie(cookie));
                }
            }

            var sessionCookie = this.Cookies.FirstOrDefault(x => x.Name == HttpConstants.SESSION_COOKIE_NAME);

            if (sessionCookie == null || !Sessions.ContainsKey(sessionCookie.Value))
            {
                var sessionId = Guid.NewGuid().ToString();
                this.Session = new Dictionary <string, string>();
                Sessions.Add(sessionId, this.Session);
                this.Cookies.Add(new Cookie(HttpConstants.SESSION_COOKIE_NAME, sessionId));
            }
            else
            {
                this.Session = Sessions[sessionCookie.Value];
            }

            this.Body = bodyBuilder.ToString();

            var parameters = this.Body.Split(new char[] { '&' }, StringSplitOptions.RemoveEmptyEntries);;

            foreach (var parameter in parameters)
            {
                var parameterParts = parameter.Split(new[] { '=' }, 2);

                var name  = parameterParts[0];
                var value = WebUtility.UrlDecode(parameterParts[1]);

                if (!FormData.ContainsKey(name))
                {
                    FormData.Add(name, value);
                }
            }
        }