我想從角度2 http服務調用我的POST端點(ASP.NET WebAPI),但端點根本沒有被擊中。我已經閱讀了幾篇文章,但是他們都沒有工作。Angular 2 - 在位置0的JSON中的意外標記 - WebAPI [HttpPost]
從我的角度2部分,我打電話給我的服務像
saveCarAuction(): void {
let respone: ResponseObject;
this.auctionservice.saveCarAuction(this.auction).subscribe(resp => {
respone = resp;
}, error => this.onError(error));
}
在auctionservice,我有
saveCarAuction(auction: CarAuction): Observable<ResponseObject> {
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
var api: string = '/CarAuction';
var url: string = this.baseApiPath + api;
return this.http.post(url, JSON.stringify(auction), options)
.map((res: Response) => res.json());
}
在我的Web API控制器,我有
[HttpPost]
[Route("api/CarAuction")]
[ActionName("PostCarAuction")]
public IHttpActionResult PostCarAuction([FromBody]CarAuctionViewModel carAuction)
{
ResponseObject response = new ResponseObject() {ResponseType = ResponseType.Success.ToString()};
//some action
return Ok(Utility.SerializeObject(response));
}
我服務電話的最終網址是
http://localhost:53796/api/CarAuction
問題是,如果我在我的操作中放置斷點,它將不會被調用。我試圖從我的服務不同的傳遞數據如下圖所示:
JSON.stringify({ carAuction: auction})
我也試圖通過屬性路由調用它(雖然我不喜歡那個),但不工作或者。我試圖在我的發佈操作中接收簡單的json字符串,並且反序列化並嘗試更新返回類型,但似乎它已決定玩一個棘手的遊戲。
在我的控制檯中的所有請求(變化)的響應低於
我真的跑出去的想法,現在做成功運行這個簡單的場景。
編輯1: 我已經包括了我傳遞給我的API
{
"carAuction": {
"carPlateNumber": null,
"carAuctionId": null,
"sellerUserId": null,
"carMakeId": null,
"carModelId": null,
"carColorId": null,
"carType": null,
"manufacturerYear": 2002,
"manufacturerMonth": 4,
"carMileage": 475000,
"ownershipCount": 3,
"ownershipType": null,
"comments": null,
"notes": null,
"terms": null,
"coverPhotoUrl": null,
"salePublicationDateTime": null,
"saleEndDateTime": null,
"auctionType": null,
"carbidPercentage": null,
"saleType": null,
"isHiddenAuction": false,
"intervalBid": 0,
"minimumPrice": 0,
"contactPersonFullName": null,
"contactPersonCompany": null,
"contactPersonPhone": null,
"contactPersonFax": null,
"contactPersonWebsite": null,
"contactPersonEmail": null,
"contactPersonAddress": null,
"hasAuctionEnded": false,
"carLocation": null,
"carMake": null,
"carModel": null,
"carColor": null,
"carAuctionMedia": [],
"id": "3F8184F7-37F4-4BD9-8B32-FDB8F65D5AB3",
"appCultureId": null,
"createdDate": null,
"createdBy": null,
"isActive": true,
"modifiedDate": null,
"modifiedBy": null
}
}
分享您的「拍賣」價值,您進入'saveCarAuction'功能? – ranakrunal9
我已經包含使用以下格式傳遞給API的json:JSON.stringify({carAuction:auction}),它的有效JSON –
@AliBaig什麼是baseApiPath? – Milad