Http 客户端不发送响应或发送 null - Angular 和 C# Api
Http client doesn't send a response or send a null - Angular and C# Api
我偶然发现了一个问题。我有一个 Angular 5 网站和一个基于 C# WebApi 的后端。
在我的网站上,我得到了一个组件,该组件支持向后端发送 post 请求以向数据库添加新实体。不幸的是,它没有发送请求或在后端收到。
bet.component.ts
import {Injectable} from '@angular/core';
import {Service} from './service';
import {Http, Headers, RequestOptions, Response} from '@angular/http';
import {AppConfig} from '../app-config';
import {Bet} from '../models/bet';
@Injectable()
export class BetService extends Service {
private url = this.config.apiUrl + '/bet';
constructor(private http: Http, private config: AppConfig) {
super();
}
addBet(bet: Bet) {
return this.http.post(this.url + '/add', bet);
}
}
BetController.cs
using System;
using System.Threading.Tasks;
using FakeBet.API.DTO;
using FakeBet.API.Services.Interfaces;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace FakeBet.API.Controllers
{
[Route("api/[controller]")]
public class BetController : Controller
{
private IBetService service;
public BetController(IBetService service)
{
this.service = service;
}
[HttpPost("[action]")]
public async Task<IActionResult> Add([FromBody] BetDTO bet)
{
try
{
await service.AddBetAsync(bet);
return Ok();
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
}
}
Url 可以。
我尝试将 bet.component.ts 更改为
return this.http.post(this.url + '/add', bet).subscribe(x => console.log('test'));
然后在后端收到响应,但 BetDto bet
为空。
虽然 bet
不为 null,并且 Angular 和 Api 中的对象的属性名称相同。
我也尝试使用 Postman 发送请求并且很顺利。这是它的样子。
谁能告诉我我的错误在哪里?干杯!
BetDTO
using System;
using FakeBet.API.Models;
namespace FakeBet.API.DTO
{
public class BetDTO
{
public Guid BetId { get; set; }
public string MatchId { get; set; }
public string UserId { get; set; }
public int BetOnTeamA { get; set; }
public int BetOnTeamB { get; set; }
}
}
bet.ts
export class Bet {
matchId: string;
userId: string;
betOnTeamA: number;
betOnTeamB: number;
}
请求使用网站:website request
使用 Postman 请求:postman request
好的,我找到了解决方案,这是我从未想过的事情。
因此,当我在 webapi 上请求资源时,我正在传递一个字段等于 "betOnTeamA" = ""
或类似内容的请求。但显然,当一个字段是这样时,.net 认为响应的整个主体都是空的。所以一个解决方案是简单地将 "betOnTeamA" = ""
更改为 "betOnTeamA" = "0"
,然后它就像一个魅力一样工作。
我偶然发现了一个问题。我有一个 Angular 5 网站和一个基于 C# WebApi 的后端。
在我的网站上,我得到了一个组件,该组件支持向后端发送 post 请求以向数据库添加新实体。不幸的是,它没有发送请求或在后端收到。
bet.component.ts
import {Injectable} from '@angular/core';
import {Service} from './service';
import {Http, Headers, RequestOptions, Response} from '@angular/http';
import {AppConfig} from '../app-config';
import {Bet} from '../models/bet';
@Injectable()
export class BetService extends Service {
private url = this.config.apiUrl + '/bet';
constructor(private http: Http, private config: AppConfig) {
super();
}
addBet(bet: Bet) {
return this.http.post(this.url + '/add', bet);
}
}
BetController.cs
using System;
using System.Threading.Tasks;
using FakeBet.API.DTO;
using FakeBet.API.Services.Interfaces;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace FakeBet.API.Controllers
{
[Route("api/[controller]")]
public class BetController : Controller
{
private IBetService service;
public BetController(IBetService service)
{
this.service = service;
}
[HttpPost("[action]")]
public async Task<IActionResult> Add([FromBody] BetDTO bet)
{
try
{
await service.AddBetAsync(bet);
return Ok();
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
}
}
Url 可以。
我尝试将 bet.component.ts 更改为
return this.http.post(this.url + '/add', bet).subscribe(x => console.log('test'));
然后在后端收到响应,但 BetDto bet
为空。
虽然 bet
不为 null,并且 Angular 和 Api 中的对象的属性名称相同。
我也尝试使用 Postman 发送请求并且很顺利。这是它的样子。
谁能告诉我我的错误在哪里?干杯!
BetDTO
using System;
using FakeBet.API.Models;
namespace FakeBet.API.DTO
{
public class BetDTO
{
public Guid BetId { get; set; }
public string MatchId { get; set; }
public string UserId { get; set; }
public int BetOnTeamA { get; set; }
public int BetOnTeamB { get; set; }
}
}
bet.ts
export class Bet {
matchId: string;
userId: string;
betOnTeamA: number;
betOnTeamB: number;
}
请求使用网站:website request
使用 Postman 请求:postman request
好的,我找到了解决方案,这是我从未想过的事情。
因此,当我在 webapi 上请求资源时,我正在传递一个字段等于 "betOnTeamA" = ""
或类似内容的请求。但显然,当一个字段是这样时,.net 认为响应的整个主体都是空的。所以一个解决方案是简单地将 "betOnTeamA" = ""
更改为 "betOnTeamA" = "0"
,然后它就像一个魅力一样工作。