/// <summary>
        /// 从请求流中反序列化构造参数值
        /// </summary>
        /// <param name="p"></param>
        /// <param name="requst"></param>
        /// <returns></returns>
        public virtual object FromBodyDeserializeObject(ParameterInfo p, HttpRequest requst)
        {
            // 从请求流中反序列化对象中,要注意三点:
            // 1、忽略参数的名称
            // 2、直接使用参数类型,不做可空类型处理
            // 3、仅支持 JSON, XML 的数据格式

            SerializeFormat format = RequestContentType.GetFormat(requst.ContentType);

            if (format == SerializeFormat.Json)
            {
                string text = requst.GetPostText();
                return(JsonExtensions.FromJson(text, p.ParameterType));
            }

            if (format == SerializeFormat.Xml)
            {
                string text = requst.GetPostText();
                return(XmlExtensions.FromXml(text, p.ParameterType));
            }

            // 仅仅是需要读取整个请求流字符串,
            // 而且目标类型已经是字符串,就没有必要反序列化了,所以就直接以字符串返回
            if (p.ParameterType == typeof(string))
            {
                return(requst.GetPostText());
            }

            throw new NotSupportedException("[FromBody]标记只能配合 JSON/XML 数据格式来使用。");
        }
Exemple #2
0
        public void Input_Product2()
        {
            string requestText = @"
POST http://www.fish-web-demo.com/Ajax/test/ComplexDataType/Input_Product.aspx HTTP/1.1
X-Result-Format: XML

" + s_ProductFormData;

            // 测试:按FROM的键值对方式输入一个对象,要求在服务端反序列化成对象,并以XML形式返回结果

            string  result = ExecuteService(requestText);
            Product p      = XmlExtensions.FromXml <Product>(result);

            AssertProductFor_Input_Product(p);
        }
        public int Input_XML(string xml)
        {
            Product p = XmlExtensions.FromXml <Product>(xml);

            return(p.ProductID);
        }
Exemple #4
0
        private LogConfig GetCloneConfig()
        {
            string xml = XmlExtensions.ToXml(WriterFactory.Config);

            return(XmlExtensions.FromXml <LogConfig>(xml));
        }