postman调用webapi

1、区分get、post、put、delete

        private static List<string> _values = new List<string> { "value1", "value2" };
        // GET api/values
        public IEnumerable<string> Get()
        {
            return _values;
        }

        // GET api/values/5
        public string Get(int id)
        {
            if (_values.Count >= id)
            {
                return _values[id - 1];
            }
            else
            {
                throw new Exception("超出长度" + _values.Count.ToString());
            }
        }

        // POST api/values
        public void Post([FromBody]string value)
        {
            if (_values.Contains(value))
            {
                throw new Exception("已存在");
            }
            else
            {
                    _values.Add(value);
            }
        }

        // PUT api/values/5
        public void Put(int id, [FromBody]string value)
        {
            if (_values.Count >= id)
            {
                for (var i = 0; i < _values.Count; i++)
                {
                    if (_values[i] == value && i != id - 1)
                    {
                        throw new Exception("已存在");
                    }
                }
                _values[id - 1] = value;
            }
            else
            {
                throw new Exception("超出长度" + _values.Count.ToString());
            }
        }

        // DELETE api/values/5
        public void Delete(int id)
        {
            if (_values.Count >= id)
            {
                _values.RemoveAt(id - 1);
            }
            else
            {
                throw new Exception("超出长度" + _values.Count.ToString());
            }
        }
View Code

2、get

 

 

 

 3、Post

 

4、Put

 

 5、delete

 

posted @ 2021-07-13 16:37  江境纣州  阅读(127)  评论(0)    收藏  举报