Wal*_*eed 6 c# asp.net-web-api asp.net-core
我正在开发 ASP.NET Core 2.1 Web API 项目。我正在尝试阅读这篇文章:https://www.c-sharpcorner.com/article/jwt-json-web-token-authentication-in-asp-net-core/但我陷入了行动。我的模型类不会绑定到输入。
[AllowAnonymous]
[HttpPost]
public IActionResult Login([FromBody] LoginVM loginVM)
{
IActionResult response = Unauthorized(); // cant reach this point, my breakpoint is here
var user = AuthenticateUser(new UserModel { });
if (user != null)
{
var tokenString = GenerateJSONWebToken(user);
response = Ok(new { token = tokenString });
}
return response;
}
public class LoginVM
{
public string Username { get; set; }
public string Password { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
您发布为x-www-form-urlencoded,但您已将[FromBody]属性应用于操作参数。这两件事从根本上是不相容的。要接受x-www-form-urlencoded(或multipart/form-data),您必须将该[FromForm]属性应用于参数。如果您有[FromBody],就像现在一样,那么您只能接受类似application/json或 的内容application/xml(如果您还启用了 XML 序列化程序)。
如果问题是您希望能够同时接受application/json 和 x-www-form-urlencoded请求机构,那么这是不可能的。您需要为每个请求正文编码一个单独的操作,尽管您可以将操作的实际内容分解为控制器上两个操作都可以使用的私有方法。
| 归档时间: |
|
| 查看次数: |
4394 次 |
| 最近记录: |