我正在尝试使用以下结构将数据发布到Web api:
Ledger Header
和list of ledger details
。这是我在Web API中针对这两个类的课程:
public class PropertyLedgerDetail
{
[Key]
public int LedgerDetailID { get; set; }
public int LedgerID { get; set; }
public int? TransactionID { get; set; }
public int? TransactionTypeID { get; set; }
public double? Amount { get; set; }
public double? PaymentAmount { get; set; }
public int Month { get; set; }
public int Year { get; set; }
[StringLength(1000)]
public string Remarks { get; set; }
public bool Voided { get; set; }
public DateTime? PostingDateTime { get; set; }
}
public class PropertyLeger
{
[Key]
public int LedgerID { get; set; }
public int CollectingAgentID { get; set; }
public int UnitID { get; set; }
public int PaymentTypeID { get; set; }
[StringLength(15)]
public string ORNumber { get; set; }
public int? VoidedLedgerID { get; set; }
public bool? Voided { get; set; }
[StringLength(100)]
public string Remarks { get; set; }
public DateTime? TransactionDateTime { get; set; }
public DateTime? PostingDateTime { get; set; }
}
这是将两个类组合在一起,以便我可以从前端接收它:
public class AddLedger
{
public PropertyLeger PropertyLedger { get; set; }
public List<PropertyLedgerDetail> PropertyLedgerDetails { get; set; }
}
在Angular前端中,这是我设置要发送的属性的方式:
从2种形式中获取值
add() {
const PropertyLedgerModel: any = {
CollectingAgentID: this.CollectingAgentID.value,
UnitID: this.unitId,
PaymentTypeID: this.PaymentTypeID.value,
ORNumber: this.ORNumber.value,
VoidedLedgerID: this.VoidedLedgerID.value,
Voided: this.Voided.value,
Remarks: this.Remarks0.value
}
const PropertyLedgerDetailsModel: any[] = this.dataSource;
const model: any = {
PropertyLedger: PropertyLedgerModel,
PropertyLedgerDetails: PropertyLedgerDetailsModel
}
this.pps.addLedger(model)
.subscribe((data: any) => {
debugger;
if (data.StatusCode === 200) {
this.ts.success(data.ReasonPhrase);
} else {
this.ts.error(data.ReasonPhrase);
}
},
err => {
this.ts.error('An error has occured in saving payment(s): ' +err);
})
}
要将其实际发送到网络api
addLedger(model: any) {
debugger;
const ep = this.rootUrl + '/api/newpropertyledger';
const PropertyLedgerModel: any = model.PropertyLedger;
const PropertyLedgerDetailsModel: any [] = model.PropertyLedgerDetails;
const Model: any = {
PropertyLedger: PropertyLedgerModel,
PropertyLedgerDetails: PropertyLedgerDetailsModel
};
return this.http.post(ep, Model);
}
,但来自前端的数据未到达网络api。我只是收到此错误:
Message:"No HTTP resource was found that matches the request URI 'http://localhost:15232/api/newpropertyledger'."
MessageDetail:"No type was found that matches the controller named 'newpropertyledger'."
请教我如何正确操作。非常感谢。
答案 0 :(得分:0)
假设您的api路由正确,即任何一种api方法都受到攻击,这应该会有帮助。
addLedger(model: any) {
debugger;
const ep = this.rootUrl + '/api/newpropertyledger';
const PropertyLedgerModel: any = model.PropertyLedger;
const PropertyLedgerDetailsModel: any [] = model.PropertyLedgerDetails;
const Model: any = {
PropertyLedger: PropertyLedgerModel,
PropertyLedgerDetails: PropertyLedgerDetailsModel
};
return this.http.post(ep, JSON.stringify(ledger));
}
public HttpResponseMessage NewPropertyLedger(string data)
{
AddLedger ledger = JsonConvert.DeserializeObject<AddLedger>(data);
}