微信小程序测试request请求webapi
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 () {
}
})
浙公网安备 33010602011771号