当返回类型为iActionResult时,component.ts类给出错误

时间:2018-09-11 20:43:11

标签: c# angular typescript asp.net-web-api .net-core

我现在有点困惑。每当我从控制器类返回output: { filename: 'build.js', path: path.resolve(__dirname, 'build') , publicPath:'/' /* <-- Add this to inject bundle at absolute path */ }, 时,一切正常。

我的controller.cs类。

void

我的service.ts类

[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
    [HttpPut("[action]")]
    public void EditEmployee(Employee employee)
    {
        if (ModelState.IsValid)
        {
            _repo.edit(employee);
            _repo.Save();
           // return Ok($"update was successful for {employee}");
        }   
      //  return BadRequest("Something Went Wrong");

    }

}

和我的component.ts类

 updateEmployee(employee) {
  let token = localStorage.getItem("jwt");
  return this._http.put('api/Employee/EditEmployee', employee, {
    headers: new HttpHeaders({
      "Authorization": "Bearer " + token,
      "Content-Type": "application/json"
  })
})

上面的代码示例按预期工作,并返回记录已成功上传

但是每当我将 controller.cs 类中的返回类型更改为onSubmit(employeeForm: NgForm) { //console.log(employeeForm.value); this._employeeService.updateEmployee(employeeForm.value).subscribe( success => { this.Message = "Record Uploaded Successfully"; }, err => this.Message = "An error Occurred" ); 时,

IActionResult

它成功更新了数据库中的记录,但在我的component.ts类中返回了发生错误

this is it on github

我想了解发生了什么以及为什么遇到此错误。

Image when controller.cs file returns void

Image when controller.cs file returns IActionResult

1 个答案:

答案 0 :(得分:2)

从controller.cs类返回一个json对象,而不是字符串文字

[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
[HttpPut("[action]")]
public IActionResult EditEmployee(Employee employee)
{
    if (ModelState.IsValid)
    {
        _repo.edit(employee);
        _repo.Save();
        return Json(new { Message="Update was successful!"});
    }   
   return BadRequest(new { Message="Something went wrong!"});
}