成都网站建设设计

将想法与焦点和您一起共享

WebApi通过后端调用传递基础类型的参数-创新互联

WebApi传递实体类型的参数,可以直接将实体序列化,然后写入请求流即可。
传递基础类型的参数却不这么方便。以下是自己踩过的坑,分享出来给大家。调用的方式是通过后端,前端的没有总结。
WebApi后端代码:

专注于为中小企业提供成都网站设计、网站建设服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业循化免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了上千多家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
public class UsersController : ApiController
    {
        private List _userList = new List() {
            new Users{ UserID=1, UserName="冯宝宝", UserEmail="fbaobao@yydy.com"},
            new Users{ UserID=2, UserName="王也", UserEmail="wangye@yydy.com"}
        };

        [HttpPost]
        public IEnumerable WithStringParam([FromBody]string name)
        {
            return _userList;
        }
        [HttpPost]
        public IEnumerable WithIntParam([FromBody]int id)
        {
            return _userList;
        }
        [HttpPost]
        public IEnumerable Post(Users user)
        {
            return _userList;
        }
        [HttpGet]
        public IEnumerable YieldGet()
        {
            foreach (var item in _userList)
            {
                yield return item;
            }
        }
    }

    public class Users
    {
        public int UserID { get; set; }
        public string UserName { get; set; }
        public string UserEmail { get; set; }
    }
}

后端调用的代码:

{
                //dynamic val = new { UserID = "1", UserName = "冯宝宝", UserEmail = "fengbaobao@yydy.com" };
                //HttpWebRequest httpWebRequest = HttpWebRequest.Create("http://localhost:51151/Api/Users/WithStringParam") as HttpWebRequest;
                HttpWebRequest httpWebRequest = HttpWebRequest.Create("http://localhost:51151/Api/Users/WithIntParam") as HttpWebRequest;
                httpWebRequest.Method = "POST";
                httpWebRequest.ContentType = "application/json";

                {
                    //string strJson = JsonConvert.SerializeObject(val);
                    //byte[] data = Encoding.UTF8.GetBytes(strJson);

                    //dynamic strVal = new { name = "aaa" };
                    //string strJson = JsonConvert.SerializeObject(strVal);
                }
                //byte[] data = Encoding.UTF8.GetBytes($"\"冯宝宝\"");//string参数
                byte[] data = Encoding.UTF8.GetBytes($"12");//int参数

                Stream requestStream = httpWebRequest.GetRequestStream();
                requestStream.Write(data, 0, data.Length);
                requestStream.Close();

                try
                {
                    using (var res = httpWebRequest.GetResponse() as HttpWebResponse)
                    {
                        if (res.StatusCode == HttpStatusCode.OK)
                        {
                            StreamReader streamReader = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
                            string result = streamReader.ReadToEnd();
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
            }

创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。


网页标题:WebApi通过后端调用传递基础类型的参数-创新互联
文章地址:http://chengdu.cdxwcx.cn/article/dijjej.html