WebApi通过后端调用传递基础类型的参数

发布时间:2020-04-10 20:53:06 作者:676386173
来源:网络 阅读:1378

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

public class UsersController : ApiController
    {
        private List<Users> _userList = new List<Users>() {
            new Users{ UserID=1, UserName="冯宝宝", UserEmail="fbaobao@yydy.com"},
            new Users{ UserID=2, UserName="王也", UserEmail="wangye@yydy.com"}
        };

        [HttpPost]
        public IEnumerable<Users> WithStringParam([FromBody]string name)
        {
            return _userList;
        }
        [HttpPost]
        public IEnumerable<Users> WithIntParam([FromBody]int id)
        {
            return _userList;
        }
        [HttpPost]
        public IEnumerable<Users> Post(Users user)
        {
            return _userList;
        }
        [HttpGet]
        public IEnumerable<Users> 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);
                }
            }
推荐阅读:
  1. Android访问WEBAPI,传递json
  2. jquery通过url传递 和 接收 参数

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

webapi 参数 基础类型

上一篇:Python_mongodb计算增加删除速度

下一篇:mysql 判断字段是否为null

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》