前一篇博客讲了Get
模式下的传递、接收参数的几种方法,现在来介绍一下Post
模式下传递、接收参数的几种方法。首先还是老样子,创建一个空的ASP.NET WebAPI
工程,在Models
文件夹下创建一个实体类:User
,代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApi.Models
{
public class User
{
///
/// 编号
///
public int Id {
get; set; }
///
/// 用户性
///
public string UserName {
get; set; }
///
/// 密码
///
public string Password {
get; set; }
}
}
如果只是传递一个参数可以使用如下方法,需要注意的是该参数的key
必须为空,同时后台必须加上[FromBody]
标识。
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>访问ASP.NET WebAPItitle>
<script src="js/jquery-3.4.1.min.js">script>
head>
<body>
<button onclick="getUserInfo()">确定button>
<script>
function getUserInfo() {
$.ajax({
type: 'post',
url: 'https://localhost:44399/api/User/GetUserInfo',
data: {
'': '张三'
},
success: function (data) {
window.alert(data);
}
})
}
script>
body>
html>
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebApi.Models;
namespace WebApi.Controllers
{
public class UserController : ApiController
{
[HttpPost]
public string GetUserInfo([FromBody] string userName)
{
return string.Format("名称:{0}", userName);
}
}
}
如果希望前端参数映射到实体类可以使用如下方法:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>访问ASP.NET WebAPItitle>
<script src="js/jquery-3.4.1.min.js">script>
head>
<body>
<button onclick="getUserInfo()">确定button>
<script>
function getUserInfo() {
$.ajax({
type: 'post',
url: 'https://localhost:44399/api/User/GetUserInfo',
data: {
id: 1,
userName: '张三',
password: '123456'
},
success: function (data) {
window.alert(data);
}
})
}
script>
body>
html>
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebApi.Models;
namespace WebApi.Controllers
{
public class UserController : ApiController
{
[HttpPost]
public string GetUserInfo([FromBody] User user)
{
return string.Format("编号:{0},用户名:{1},密码:{2}", user.Id, user.UserName, user.Password);
}
}
}
这种方法比较推荐,无论是一个参数还是多个参数都能使用,代码如下:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>访问ASP.NET WebAPItitle>
<script src="js/jquery-3.4.1.min.js">script>
head>
<body>
<button onclick="getUserInfo()">确定button>
<script>
function getUserInfo() {
$.ajax({
type: 'post',
url: 'https://localhost:44399/api/User/GetUserInfo',
data: {
id: 1,
userName: '张三',
password: '123456'
},
success: function (data) {
window.alert(data);
}
})
}
script>
body>
html>
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebApi.Models;
namespace WebApi.Controllers
{
public class UserController : ApiController
{
[HttpPost]
public string GetUserInfo([FromBody] JObject obj)
{
int id = int.Parse(obj["id"].ToString());
string userName = obj["userName"].ToString();
string password = obj["password"].ToString();
return string.Format("编号:{0},用户名:{1},密码:{2}", id, userName, password);
}
}
}