- 框架:asp.net core webapi
- asp.net core webapi接收参数,请求变量设置
目录
- 接收multipart/form-data、application/x-www-form-urlencoded类型参数
- 接收URL参数
- 接收上传的文件
- webapi接收json参数
接收multipart/form-data、application/x-www-form-urlencoded类型参数
Post ([FromForm]TokenRequestInput user)
可以接收发送类型为multipart/form-data、application/x-www-form-urlencoded的数据
[HttpPost]public async Task<IActionResult> ChangePhoneNum([FromForm] TokenRequestInput user){ return Ok(11222);}/// <summary>/// 用户信息/// </summary>public class TokenRequestInput{/// <summary>/// 微信 用户的openid/// </summary>public string? openid { get; set; }/// <summary>/// 微信头像图片,base64字符串/// </summary>public string? head_img_base64 { get; set; }/// <summary>/// 微信昵称/// </summary>public string? nichen { get; set; }}
接收URL参数
请求地址
http://localhost:5170/api/User/GetToken?code=22222
[HttpGet]public async Task<IActionResult> GetToken(string code){var result = await wxAppletLoginBll.GetLoginToken(code);return Ok(result);}
接收上传的文件
IFormFile file这个参数是接收文件,mimeType=multipart/form-data
参数userId,通过url参数传入
/// <summary>
/// 接收上传的文件
/// </summary>
/// <param name="file">文件二进制</param>
/// <param name="userId">url参数</param>
/// <returns></returns>
[HttpPost]
public async Task<IActionResult> GetAdd(IFormFile file, string userId)
{ return Ok("ok");
}
webapi接收json参数
发送json参数
{"openid": "string","head_img_base64": "string","nichen": "string"
}
[HttpPost]public async Task<IActionResult> AddUser(TokenRequestInput user){ return Ok(3344);}