[英]how to read posted FormUrlEncodedContent in asp.net core web api?
in asp.net core web api
i have a POST
method that get username and password in FormUrlEncodedContent
format from client. 在
asp.net core web api
我有一个POST
方法,从客户端获取FormUrlEncodedContent
格式的用户名和密码。 But the "entry" parameter is null
. 但“entry”参数为
null
。
how can i access the username and password in Login method in web api ? 如何在web api中的Login方法中访问用户名和密码?
here is my code: 这是我的代码:
client: 客户:
public async Task<string> login2(string command , string username , string password)
{
string exist = string.Empty;
FormUrlEncodedContent dataForm = new FormUrlEncodedContent(new[] {
new KeyValuePair<string,string>("username",username),
new KeyValuePair<string, string>("password",password)
});
var resp = await http.PostAsync(command,dataForm);
exist = await resp.Content.ReadAsStringAsync();
return exist;
}
server : 服务器:
public IActionResult Login([FromForm] string entry)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
Console.WriteLine(entry);
return Ok(entry);
}
Create a model to hold the posted information 创建模型以保存发布的信息
public class LoginModel {
public string username { get; set; }
public string password { get; set; }
}
and update the action to expect that from the from 并更新动作以期望来自
public IActionResult Login([FromForm] LoginModel entry) {
if (!ModelState.IsValid) {
return BadRequest(ModelState);
}
// access the username and password
var username = entry.username;
var password = entry.password;
return Ok();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.