【问题标题】:Angular 8 vs .NetCore WebAPI passing object as parameterAngular 8 vs .Net Core Web API 将对象作为参数传递
【发布时间】:2021-02-07 19:56:41
【问题描述】:

我正在尝试从 Angular 8 访问 .Net 核心 WebApi,我可以使用 HttpGet 方法,但使用 post 方法,同时将寄存器数据作为对象传递是不允许的。显示 400 问题。下面是为命中而编写的代码。请帮忙。 RegisterCompoent.ts.

    this.userService.RegisterUser(this.userForm.value).subscribe((data) => {
      if (data) {
        alert("Registration successfull, go ahead and login");
        this.router.navigate(['']);
      }
    }, error => this.errormessage = error)
  }```
UserService.ts

 ```httpOptions = {
    headers: new HttpHeaders({
      'Content-Type': 'application/json'
    })
  }
  RegisterUser(userDetails: any ):Observable<UserInfo> {
   
    // return this.http.post<UserInfo>('https://localhost:44322/api/User/RegisterUser/',userDetails,this.httpOptions)
    return this.http.post<UserInfo>('https://localhost:44322/api/User/RegisterUser/',JSON.stringify(userDetails),this.httpOptions)
  }```


.Net Core WebAPI RegisterUser

```[ApiController]
    [Route("api/User/[Action]")]
    public class UserController : ControllerBase
    {
        SqlRepository repository = new SqlRepository();
        [HttpPost]
        public int RegisterUser(UserInfo userInfo)
        {
            return repository.RegisterUsers(userInfo);
        }
        //[HttpPost]
        //public int RegisterUser([FromBody] string userInfo)
        //{
        //    UserInfo userDetails = JsonConvert.DeserializeObject<UserInfo>(userInfo.ToString());
        //    return repository.RegisterUsers(userDetails);
        //}```

please help the error it shows
[![enter image description here][1]][1]


  [1]: https://i.stack.imgur.com/vvTts.png

【问题讨论】:

标签: angular asp.net-core-webapi


【解决方案1】:

您是否尝试将 userDetails 中的 any 类型替换为字符串:

 RegisterUser(userDetails: string ):Observable<UserInfo> {
   
    return this.http.post<UserInfo>('https://localhost:44322/api/User/RegisterUser/',userDetails,this.httpOptions)
   }```

【讨论】:

    【解决方案2】:

    注意:我没有对其进行测试,但这应该可以帮助您解决问题。

    嗨@SAI,如果您尝试访问的主机/端口是正确的,我会尝试进行以下更改:

    删除 URL 末尾多余的“/”,同时删除 JSON.stringify 调用

    RegisterUser(userDetails: string ):Observable<UserInfo> {   
       return this.http.post<UserInfo>('https://localhost:44322/api/User/RegisterUser',userDetails, this.httpOptions)
    }
    

    更新控制器的路由配置(另外,检查我添加到您的操作中的 cmets)

    [ApiController]
    [Route("api/[controller]")]
    public class UserController : ControllerBase
    {
        SqlRepository repository = new SqlRepository();
    
        [HttpPost]
        public int RegisterUser(UserInfo userInfo)
        {
            // some details here. I am guessing this is an async operation
            // In which case I think you need to change this code also to wait
            // for the response and the return type of this action to Task<int>
            return repository.RegisterUsers(userInfo);
        }
    
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-17
      • 2019-05-28
      • 2021-03-28
      • 2021-06-13
      相关资源
      最近更新 更多