1. 实例

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using wxweb.Areas.API.Models;

namespace wxweb.Areas.API.Controllers
{
    public class ProductController : 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 }
      };

        Product[] products_post = new Product[]
     {
            new Product { Id = 1, Name = "name01", Category = "Category01", Price = 10 },
            new Product { Id = 2, Name = "name02", Category = "Category02", Price = 20M },
            new Product { Id = 3, Name = "name03", Category = "Category03", Price = 30M }
     };

        /// <summary>
        /// get 无参数传参
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IEnumerable<Product> GetAllProducts()
        {
           return products;
        }

        /// <summary>
        /// get 单参数传参
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Route("api/Product/GetProductById")]
        [HttpGet]
        public IHttpActionResult GetProductById(string id)
        {
            var product = products.FirstOrDefault((p) => p.Id == Convert.ToInt32(id));
            if (product == null)
            {
                return NotFound();
            }
            return Ok(product);
        }

        /// <summary>
        /// get多参数传参
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpGet]
        public string para_get_base(string id,string name)
        {
            return "id:"+id+"   name:"+name;
        }

        /// <summary>
        /// get form传参
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        [Route("api/Product/para_get_form")]
        [HttpGet]
        public string para_get_form([FromUri]Product p)
        {
            return "p.Name:" + p.Name + "   p.Price:" + p.Price;
        }

        /// <summary>
        /// post 无参数传参
        /// </summary>
        /// <returns></returns>
        [Route("api/Product/GetProducts")]
        [HttpPost]
        public IEnumerable<Product> GetProducts()
        {
          
            return products_post;
        }

        /// <summary>
        /// post 单个参数传参
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Route("api/Product/GetProduct")]
        [HttpPost]
        public IHttpActionResult GetProduct([FromBody]string id)
        {
            var product = products_post.FirstOrDefault((p) => p.Id ==Convert.ToInt32( id));
            if (product == null)
            {
                return NotFound();
            }
            return Ok(product);
        }
        /// <summary>
        /// post 多参数传参
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [Route("api/Product/para_post_base")]
        [HttpPost]
        public string para_post_base(dynamic obj)
        {
            string id = obj["id"].ToString();
            string name = obj["name"].ToString();
            return "id:" + id + "   name:" + name;
        }

        /// <summary>
        /// post form传参
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        [Route("api/Product/para_post_form")]
        [HttpPost]
        public string para_post_form(Product p)
        {
            return "p.Name:" + p.Name + "   p.Price:" + p.Price;
        }

        /// <summary>
        /// post base+form传参
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        [Route("api/Product/para_post_baseform")]
        [HttpPost]
        public string para_post_baseform(dynamic obj)
        {
            var parapost = Convert.ToString(obj.parapost);
            Product p = Newtonsoft.Json.JsonConvert.DeserializeObject<Product>(Convert.ToString(obj.formdata));
            return "parapost:"+parapost+ "  p.Name:" + p.Name + "   p.Price:" + p.Price;
        }

        /// <summary>
        /// post 数组参数
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [Route("api/Product/post_array")]
        [HttpPost]
        public string post_array(string[] ids)
        {
            string result = "";
            for (int i = 0; i < ids.Length; i++) {
                result += "  :" + ids[i];
            }
            return result;
        }

        /// <summary>
        /// post 实体数组参数
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [Route("api/Product/post_ojblist")]
        [HttpPost]
        public string post_ojblist(List<Product> plist)
        {
            string result = "";
            for (int i = 0; i < plist.Count; i++)
            {
                result +="    <br/>||"+i+ ":name:" + plist[i].Name+"  price:"+plist[i].Price;
            }
            return result;
        }
    }
}
webapi

相关文章: