WebApi Post参数对象,服务器端参数对象为空的问题

最近在研究WebApi,在实际的工作中遇到了一个问题:在将参数对象MSG2的实例通过Post至服务器端的时候,

 public static string SetMessageOperationResult(MSG2 model)
        {
            string result = string.Empty;
            if (model == null) return result;          
            Hashtable ht = new Hashtable();         
            ht.Add("Authorization", string.Format("Bearer {0}", _accToken));             

            var content = JsonConvert.SerializeObject(model);          
            string url = string.Format("{0}{1}", _baseUrl, _setMessageOperationResult);
            result = HttpHelper.PostData(content, url, _timeout, ht);
            return result;
        }
服务器端接收到的参数实例化结果总是为空:

 [HttpPost]
        public async Task SetMessageOperationResult(MSG2 MSG2)
        {
         if(MSG2 == null) //一直成立


 我一遍又一遍的检查代码,尝试着各种解决办法,包括 
  [FromBody] \ [FromURL] ,各问题依旧。。。。。。 
  

一直到了下午,吃过了午饭,在小伙伴的一次次帮助下,最终我发现了问题的所在:

 public static string SetMessageOperationResult(MSG2 model)
        {
            string result = string.Empty;
            if (model == null) return result;          
            Hashtable ht = new Hashtable();         
            ht.Add("Authorization", string.Format("Bearer {0}", _accToken));             

            var content = JsonConvert.SerializeObject(model);          
            string url = string.Format("{0}{1}", _baseUrl, _setMessageOperationResult);
            result = HttpHelper.PostData(content, url, _timeout, ht, "application/json", "text/json");
            return result;
        }
如果红色的两个参数不传,我的方法默认这两个参数的值为:"application/x-www-form-urlencoded"  和   "application/json",则服务器端将会获取不到客户端传递过来的值


如果按照红色的值进行传递,那么服务器端参数对象就会接收到下图的说要传递的值,


   public static string PostData(string request, string url, int timeout, Hashtable ht, string reqType = "application/x-www-form-urlencoded", string resType = "application/json")
        {

            string responseString = string.Empty;
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
            webRequest.ContentType = reqType;// + ";charset=\"utf-8\""
            webRequest.Accept = resType;// resType;

            webRequest.Method = "POST";
            webRequest.Timeout = timeout * 1000;


            try
            {


                foreach (DictionaryEntry de in ht)
                {
                    webRequest.Headers.Add(de.Key.ToString(), de.Value.ToString());
                }
                byte[] bytes = Encoding.GetEncoding("utf-8").GetBytes(request);
                webRequest.ContentLength = bytes.Length;
                webRequest.GetRequestStream().Write(bytes, 0, bytes.Length);
                HttpWebResponse response;//= (HttpWebResponse)webRequest.GetResponse();
                try
                {
                    response = (HttpWebResponse)webRequest.GetResponse();
                }
                catch (WebException ex)
                {

                    response = (HttpWebResponse)ex.Response;
                    using (StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding("utf-8")))
                    {
                        responseString = reader.ReadToEnd();
                    }
                    throw new Exception(responseString);
                }


                using (StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding("utf-8")))
                {
                    responseString = reader.ReadToEnd();
                }

                return responseString;
            }

            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
        }




你可能感兴趣的:(学习笔记)