none
请问在web api中如何实现post方式的查询? RRS feed

  • 问题

  •    public IHttpActionResult GetTcUserNew  (string uid, string PassWord, string callback = "")
            {


                string sql = "select uid,uname,ustate from tc_user_new where uid='" + uid + "' and pwdcompare('" + PassWord + "',upass,0)=1";
                DataTable Data = PublicFunction.GetStudyData(sql);

                //查询表达式的写法
                //IEnumerable<TcUserNew>
                var TcUserNew =
                   from dt1 in Data.AsEnumerable() select new TcUserNew { uid = Convert.ToDecimal(dt1["uid"]), uname = dt1["uname"].ToString(), ustate = Convert.ToInt16(dt1["ustate"]) };
                if (TcUserNew == null)
                    return NotFound();
                else
                    return Ok(TcUserNew);
            }

    请问是不是加一个[HttpPost] 就可以了?

    不过我使用安卓的xutil访问不成功

    2016年4月25日 7:36

答案

  • 在前面加上[HttpPost]就可以了,但是参数必须是一个,而且只能是class. 例如你的

    class UserPara

    {

        public string uid {get;set;}

        public string PassWord{get;set;}

        public string callback {get;set;}

    }

    然后

    [HttpPost]

    public IHttpActionResult GetTcUserNew  (UserPara para)

    下面是我的代码, 在测试工程里测试没有问题:

       public class ProductsController : ApiController
        {
            Product[] products = new Product[] 
            { 
                new Product { Id = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1 }, 
                new Product { Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M }, 
                new Product { Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M } 
            };
    
            public IEnumerable<Product> GetAllProducts()
            {
                return products;
            }
    
            public IHttpActionResult GetProduct(int id)
            {
                var product = products.FirstOrDefault((p) => p.Id == id);
                if (product == null)
                {
                    return NotFound();
                }
                return Ok(product);
            }
    
            
            [HttpPost]
            public IHttpActionResult GetProduct(ProductSearchParameters para)
            {
                var product = products.FirstOrDefault((p) => p.Name.Contains(para.Name));
                if (product == null)
                {
                    return NotFound();
                }
                return Ok(product);
            }
    
        }

        public class ProductSearchParameters
        {
            public string Name { get; set; }
        }

    我使用的Advanced REST client测试的。给url http://localhost:62848/api/products 发送post action,Post的body是 name=mmer, 结果如下

    2016年4月26日 2:37
  • Hi,

    默认的WebApi范例:

    [Authorize]
    
        public class ValuesController : ApiController
    
        {
    
            // GET api/values
    
            public IEnumerable<string> Get()
    
            {
    
                return new string[] { "value1", "value2" };
    
            }
    
            // GET api/values/5
    
            public string Get(int id)
    
            {
    
                return "value";
    
            }
    
           // POST api/values
    
            public void Post([FromBody]string value)
    
            {
    
            }
    
            [HttpPost]
    
            public HttpResponseMessage Post([FromBody] Test test)
    
            {
    
                // Save Code will be here
    
                return new HttpResponseMessage(HttpStatusCode.OK);
    
            }
    
            // PUT api/values/5
    
            public void Put(int id, [FromBody]string value)
    
            {
    
            }
    
            // DELETE api/values/5
    
            public void Delete(int id)
    
            {
    
           }
    
        }
    

    你可以试着把参数拼成一串string,然后在webapi中拆解出来。

    例如:

    [HttpPost]
    
    [Authorize]
    
    public string GetStockList([FromBody]string jsonString)
    
    {
    
        JObject jObject = JObject.Parse(jsonString);
    
        page = Convert.ToInt32(jObject["page"].ToString());
    
        pageSize = Convert.ToInt32(jObject["pageSize"].ToString());    
    
    }
    

    另外,安卓调用的话,你可以参考下面的链接:

    Android调用WebAPI之传参、文件

    Regards,

    Moonlight


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    2016年4月26日 2:55

全部回复

  • 在前面加上[HttpPost]就可以了,但是参数必须是一个,而且只能是class. 例如你的

    class UserPara

    {

        public string uid {get;set;}

        public string PassWord{get;set;}

        public string callback {get;set;}

    }

    然后

    [HttpPost]

    public IHttpActionResult GetTcUserNew  (UserPara para)

    下面是我的代码, 在测试工程里测试没有问题:

       public class ProductsController : ApiController
        {
            Product[] products = new Product[] 
            { 
                new Product { Id = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1 }, 
                new Product { Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M }, 
                new Product { Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M } 
            };
    
            public IEnumerable<Product> GetAllProducts()
            {
                return products;
            }
    
            public IHttpActionResult GetProduct(int id)
            {
                var product = products.FirstOrDefault((p) => p.Id == id);
                if (product == null)
                {
                    return NotFound();
                }
                return Ok(product);
            }
    
            
            [HttpPost]
            public IHttpActionResult GetProduct(ProductSearchParameters para)
            {
                var product = products.FirstOrDefault((p) => p.Name.Contains(para.Name));
                if (product == null)
                {
                    return NotFound();
                }
                return Ok(product);
            }
    
        }

        public class ProductSearchParameters
        {
            public string Name { get; set; }
        }

    我使用的Advanced REST client测试的。给url http://localhost:62848/api/products 发送post action,Post的body是 name=mmer, 结果如下

    2016年4月26日 2:37
  • Hi,

    默认的WebApi范例:

    [Authorize]
    
        public class ValuesController : ApiController
    
        {
    
            // GET api/values
    
            public IEnumerable<string> Get()
    
            {
    
                return new string[] { "value1", "value2" };
    
            }
    
            // GET api/values/5
    
            public string Get(int id)
    
            {
    
                return "value";
    
            }
    
           // POST api/values
    
            public void Post([FromBody]string value)
    
            {
    
            }
    
            [HttpPost]
    
            public HttpResponseMessage Post([FromBody] Test test)
    
            {
    
                // Save Code will be here
    
                return new HttpResponseMessage(HttpStatusCode.OK);
    
            }
    
            // PUT api/values/5
    
            public void Put(int id, [FromBody]string value)
    
            {
    
            }
    
            // DELETE api/values/5
    
            public void Delete(int id)
    
            {
    
           }
    
        }
    

    你可以试着把参数拼成一串string,然后在webapi中拆解出来。

    例如:

    [HttpPost]
    
    [Authorize]
    
    public string GetStockList([FromBody]string jsonString)
    
    {
    
        JObject jObject = JObject.Parse(jsonString);
    
        page = Convert.ToInt32(jObject["page"].ToString());
    
        pageSize = Convert.ToInt32(jObject["pageSize"].ToString());    
    
    }
    

    另外,安卓调用的话,你可以参考下面的链接:

    Android调用WebAPI之传参、文件

    Regards,

    Moonlight


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    2016年4月26日 2:55