可能是因为在发送请求到后端时,参数没有被正确地传递。确保在前端angular代码中定义了正确的参数,并在后端ASP.NET代码中正确地解析这些参数。
示例:
Angular代码:
let data = {
name: 'John',
age: 25
};
this.http.post('https://example.com/api', data).subscribe((response) => {
console.log(response);
});
ASP.NET代码:
public IActionResult PostData([FromBody]UserModel user)
{
if (user == null) {
return BadRequest("Invalid data");
}
// do something with user data
}
public class UserModel
{
public string Name { get; set; }
public int Age { get; set; }
}
在这个例子中,我们将一个名为“data”的包含用户名称和年龄的对象发送到URL“https://example.com/api”。当在ASP.NET中接收到请求时,我们使用名称为“UserModel”的模型类来接收用户对象。如果请求中的数据为空,我们将返回400 Bad Request响应并显示“Invalid data”消息。在实际的应用程序中,您可以修改PostData方法来执行您需要的操作。