1. 程式人生 > >WebApi 方法的參數類型總結。

WebApi 方法的參數類型總結。

異步 erro 單個 eas easy ret sign soft yui

1:[HttpGet]

2:[HttpPost]

ApiController中方法參數類型之單個參數。

      /// <summary>
        /// post,一個參數。用[FromBody]去http的請求體裏面去取參數。
        /// Client請求成功
        /// </summary>
        /// <param name="hname"></param>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult PostStudentInforOnePara([FromBody]string
hname) { List<StudentModel> stlists = new List<StudentModel>(); stlists.Add(new StudentModel { hno = "1001", hname = "", hobject = "WebApi", hscore = "90" }); stlists.Add(new StudentModel { hno = "1002", hname = "龍大", hobject = "Ajax", hscore = "80
" }); stlists.Add(new StudentModel { hno = "1003", hname = "longdb", hobject = "SignalR", hscore = "88" }); StudentModel retstu = stlists.FirstOrDefault(stu => stu.hname == hname); return Json<StudentModel>(retstu);//, Newtonsoft.Json.JsonSerializer.CreateDefault.stlists);
}

Client 中Ajax方式調用:

     //POST WebApi之一個參數的方法。成功
     function SumittomainPostOne() {
         $.ajax({
             url: ‘http://192.168.0.102/webApiDemo/api/WebApiTest/PostStudentInforOnePara‘,
             type: ‘post‘,
             data: { "": "longdb" },//一個參數時,必須這樣寫,webapi中http的請求體裏面去取參數才能取到。       
             async: true,//異步
             success: function (data) //成功後的回調方法
             {
                 alert(JSON.stringify(data))//彈出框  
                 window.location.href = "EasyUILoutMain.aspx";//可以跳轉.
             },
             error: function () {
                 alert("失敗!");
                 window.location.href = "EasyUILoutMain.aspx";//可以跳轉.
             }
         });
     }

WebApi 方法的參數類型總結。