RESTful

最近学习了下WebApi,WebApi是RESTful风格,根据请求方式决定操作。以博客的形式写出来,加深印象以及方便以后查看和复习。

1、首先我们使用VS创建一个空的WebApi项目

2、新建实体以及控制器类

  1. 1 public class Product
  2. 2 {
  3. 3 public int Id { set; get; }
  4. 4 public string Name { set; get; }
  5. 5 public string Description { set; get; }
  6. 6 }
  1. 1 public class HomeController : ApiController
  2. 2 {
  3. 3 static List<Product> modelList = new List<Product>()
  4. 4 {
  5. 5 new Product(){Id=1,Name="电脑",Description="电器"},
  6. 6 new Product(){Id=2,Name="冰箱",Description="电器"},
  7. 7 };
  8. 8
  9. 9 //获取所有数据
  10. 10 [HttpGet]
  11. 11 public List<Product> GetAll()
  12. 12 {
  13. 13 return modelList;
  14. 14 }
  15. 15
  16. 16 //获取一条数据
  17. 17 [HttpGet]
  18. 18 public Product GetOne(int id)
  19. 19 {
  20. 20 return modelList.FirstOrDefault(p => p.Id == id);
  21. 21 }
  22. 22
  23. 23 //新增
  24. 24 [HttpPost]
  25. 25 public bool PostNew(Product model)
  26. 26 {
  27. 27 modelList.Add(model);
  28. 28 return true;
  29. 29 }
  30. 30
  31. 31 //删除
  32. 32 [HttpDelete]
  33. 33 public bool Delete(int id)
  34. 34 {
  35. 35 return modelList.Remove(modelList.Find(p => p.Id == id));
  36. 36 }
  37. 37
  38. 38 //更新
  39. 39 [HttpPut]
  40. 40 public bool PutOne(Product model)
  41. 41 {
  42. 42 Product editModel = modelList.Find(p => p.Id == model.Id);
  43. 43 editModel.Name = model.Name;
  44. 44 editModel.Description = model.Description;
  45. 45 return true;
  46. 46 }
  47. 47 }

3、新建html页面使用ajax操作

  1. 1 <!DOCTYPE html>
  2. 2 <html xmlns="http://www.w3.org/1999/xhtml">
  3. 3 <head>
  4. 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. 5 <title>Demo</title>
  6. 6 <script src="/Scripts/jquery-1.10.2.js"></script>
  7. 7 <script type="text/javascript">
  8. 8 $(function () {
  9. 9 add();
  10. 10 update();
  11. 11 find();
  12. 12 remove();
  13. 13 getAll();
  14. 14 });
  15. 15 function getAll() {
  16. 16 $.ajax({
  17. 17 url: "api/Home/",
  18. 18 type: \'GET\',
  19. 19 success: function (data) {
  20. 20 console.log(data);
  21. 21 }
  22. 22 });
  23. 23 }
  24. 24
  25. 25 function find() {
  26. 26 $.ajax({
  27. 27 url: "api/Home/1",
  28. 28 type: \'GET\',
  29. 29 success: function (data) {
  30. 30 console.log(data);
  31. 31 }
  32. 32 });
  33. 33 }
  34. 34
  35. 35 function add() {
  36. 36 $.ajax({
  37. 37 url: "api/Home/",
  38. 38 type: "POST",
  39. 39 data: { "Id": "3", "Name": "电磁炉", "Description": "电器" },
  40. 40 success: function (data) {
  41. 41 console.log(data);
  42. 42 }
  43. 43 });
  44. 44
  45. 45 }
  46. 46
  47. 47 function update() {
  48. 48 $.ajax({
  49. 49 url: "api/Home/",
  50. 50 type: \'PUT\',
  51. 51 data: { "id": "1", "Name": "洗衣机", "Description": "家具" },
  52. 52 success: function (data) {
  53. 53 console.log(data);
  54. 54 }
  55. 55 });
  56. 56 }
  57. 57
  58. 58 function remove() {
  59. 59 $.ajax({
  60. 60 url: "api/Home/1",
  61. 61 type: \'DELETE\',
  62. 62 success: function (data) {
  63. 63 console.log(data);
  64. 64 }
  65. 65 });
  66. 66 }
  67. 67 </script>
  68. 68 </head>
  69. 69 <body>
  70. 70 <h1>WebApi基本操作</h1>
  71. 71 </body>
  72. 72 </html>

4、通过开发人员工具可以看到如下

WebApi默认是以XML格式返回,但是一般我们需要返回Json,通过在Global.asax里的Application_Start方法中加上如下代码可以移除XML,让其只返回Json

  1. GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();

有一个需要注意的是,如果在参数写的是实体类,那么需要加上相应的特性(post、put请求不需要),如下

  1. 1 [HttpGet]
  2. 2 public User Get([FromUri]User user)
  3. 3 {
  4. 4 return user;
  5. 5 }

 

版权声明:本文为zuqing原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/zuqing/p/5463320.html