using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; namespace TestWebAPI.Controllers { public class ValuesController : ApiController { // GET api/values public IEnumerable<string> Get() { return new string[] { "value1", "value2" }; } [HttpPost] public object FindID(dynamic obj) { var strName = Convert.ToString(obj.NAME) + Convert.ToString(obj.DES); return strName; } // GET api/values/5 public string Get(int id) { return "value"; } //// POST api/values //public void Post([FromBody]string value) //{ //} // PUT api/values/5 public void Put(int id, [FromBody]string value) { } // DELETE api/values/5 public void Delete(int id) { } } }
注意:如果請求的方式不是以“”post“”開頭的方法,例如:“”FindID“”這個時候把post的方法注釋掉,要不然找不到FindID方法,默認去找post開頭的方法。,注意post方式傳遞參數的寫法。
// pages/index/index.js Page({ /** * 頁面的初始數據 */ data: { }, requestClick(e) { wx.request({ url: 'http://localhost:49523/api/values/FindID', data: JSON.stringify({ NAME: "Jim", DES: "備注" }), header:{"content":"application/json"}, method:"POST", success:function(res){ console.log(res); wx.showModal({ title: 'testRequest', content: res.data, }) } }) }, /** * 生命周期函數--監聽頁面加載 */ onLoad: function (options) { }, /** * 生命周期函數--監聽頁面初次渲染完成 */ onReady: function () { }, /** * 生命周期函數--監聽頁面顯示 */ onShow: function () { }, /** * 生命周期函數--監聽頁面隱藏 */ onHide: function () { }, /** * 生命周期函數--監聽頁面卸載 */ onUnload: function () { }, /** * 頁面相關事件處理函數--監聽用戶下拉動作 */ onPullDownRefresh: function () { }, /** * 頁面上拉觸底事件的處理函數 */ onReachBottom: function () { }, /** * 用戶點擊右上角分享 */ onShareAppMessage: function () { } })