构建WebApi服务的步骤为:
    1,创建空的WebApi项目
    2,添加空的WebApi控制器(控制器存放位置在Controllers文件夹中)
    3,在控制器中添加方法(用于处理客户端发送的请求)
    备注:下面只演示代码,创建过程缺失

    创建控制器,编写处理请求的方法

    1. public class GetResponseController:ApiController
    2. {
    3. //标识该方法属于GET请求
    4. [HttpGet]
    5. //标识该方法的请求路径
    6. [Route("api/showname")]
    7. public string ShowName(string userName)
    8. {
    9. return string.Format("您输入的姓名是:{0}",userName);
    10. }
    11. [HttpPost]
    12. [Route("api/getstudentinfo")]
    13. public Student GetStudentInfo([FromBody]Student student)
    14. {
    15. List<Student> StudentList = Data.DataHelper.IntiStudentList(); //加载数据集
    16. if(StudentList!=null&&StudentList.Any())
    17. {
    18. Student currentStudent =StudentList.Where(c=>c.StudentId.Equals(student.StudentId)
    19. &&c=>c.StudentName.Equals(student.StudentName)).FirstOrDefault();
    20. if(currentStudent!=null)
    21. {
    22. return currentStudent;
    23. }
    24. }
    25. return null;
    26. }
    27. }

    备注:如果使用[FromBody]去传递string类型的参数,服务端接收到的值一直都是null,因此最好是dto模型来进行传递即可。

    数据源

    1. public class DataHelper
    2. {
    3. public static List<Student> IntiStudentList()
    4. {
    5. return new List<Student>(){
    6. new Student(){StudentId="001",StudentName="Rotu"},
    7. new Student(){StudentId="002",StudentName="Jack"},
    8. };
    9. }
    10. }
    11. [Serializable]
    12. public class Student
    13. {
    14. public string StudentId{get;set;}
    15. public string StudentName{get;set;}
    16. }

    页面发送请求

    1. <!DOCTYPE html>
    2. <html xmlns="http://www.w3.org/1999/xhtml">
    3. <head>
    4. <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
    5. <title></title>
    6. <script type="text/javascript" src="/Script/jquery-1.10.2.min.js"></script>
    7. <script type="text/javascript">
    8. $(function(){
    9. //使用ajax发送get请求
    10. $("#GetRequest").click(function(){
    11. var userName =$("#UserName").val();
    12. if(userName=="")
    13. {
    14. alert("请输入姓名");
    15. return;
    16. }
    17. $.ajax
    18. ({
    19. url:"/ap/showname?username="+userName,
    20. type:"GET",
    21. success:function(data)
    22. {
    23. alert(data);
    24. }
    25. });
    26. });
    27. //使用ajax发送post请求
    28. $("#PostRequest").click(function(){
    29. var studentId =$("#StudentId").val();
    30. var studentName =$("#StudentName").val();
    31. if(studentId==""||studentName==="")
    32. {
    33. alert("编号或姓名为空");
    34. return;
    35. }
    36. var student ={
    37. StudentId:studentId,
    38. StudentName:studentName
    39. };
    40. $.ajax({
    41. url:"/api/getstudentinfo",
    42. type:"POST",
    43. dataType:"JSON",
    44. data:student,
    45. success:function(data)
    46. {
    47. alert(data);
    48. }
    49. });
    50. });
    51. })
    52. </script>
    53. </head>
    54. <body>
    55. 姓名:<input type="text" id="UserName"/>
    56. <button id="GetRequest">发送GET请求</button>
    57. 编号:<input type="text" id="StudentId" name="StudentId"/>
    58. 姓名:<input type="text" id="StudentName" name="StudentName"/>
    59. <button id="PostRequest">发送POST请求</button>
    60. </body>
    61. </html>