【发布时间】:2020-11-26 17:32:36
【问题描述】:
我是 Angular 新手,在尝试从 Angular 项目向 Web api 发布字符串时,Web api Post 方法中的参数始终为 null。如果我使用 [FromBody],则 post 方法本身不会被调用。如果我从 Post 方法中删除了 [FromBody]。该方法被调用,但参数的值为 null。任何人都可以在这里帮助我!!
SendTODB(db:Event)
{
this.http.post('https://localhost:44301/Student',this.inpuctstexts).subscribe((data)=>(<HTMLInputElement>db.target).value);
}
在我的 WebApi 中:
[ApiController]
[Route("[controller]")]
public class StudentController : ControllerBase
{
[HttpGet]
public ActionResult<List<Student>> StudentList()
{
List<Student> student = new List<Student>();
student.Add(new Student() { StudentName = "Mike", Marks = "200" });
student.Add(new Student() { StudentName = "Jack", Marks = "250" });
student.Add(new Student() { StudentName = "Jacob", Marks = "300" });
student.Add(new Student() { StudentName = "John", Marks = "500" });
return (student);
}
[HttpPost]
public ActionResult<string> student(string input)
{
return input;
}
}
【问题讨论】:
-
调用本身似乎是时间,在调用此行之前检查正文是否为空。
-
邮政编码此处为格式化文本。不通过图片或第三方服务链接。
-
变量(this.inpuctstexts)不为空。我已经使用 console.log(this.inpuctstexts) 进行了检查。
-
请确保您的 :this.inpuctstexts 与您的 [Frombody] Myclass myclass 是同一个对象。如果您的 inpuctstexts 只是一个字符串,请将其包装在一个对象中
-
嗨@crashmstr ..我已经在格式化文本中添加了代码
标签: angular typescript http webapi