C# 无法在C代码中显示API响应消息
我将从下面的请求生成Way Bill API。下面请求的响应在“Postmen”中显示良好,但同时,当我从C代码执行下面的请求时,它会显示一条错误消息,“远程服务器返回一个错误:(400)错误请求。” 我想知道如何用C代码显示邮递员错误消息 网址:-C# 无法在C代码中显示API响应消息,c#,json,C#,Json,我将从下面的请求生成Way Bill API。下面请求的响应在“Postmen”中显示良好,但同时,当我从C代码执行下面的请求时,它会显示一条错误消息,“远程服务器返回一个错误:(400)错误请求。” 我想知道如何用C代码显示邮递员错误消息 网址:- https://ewbbackend-preprodpub-http.internal.cleartax.co/gst/v0.1/taxable_entities/1c74ddd2-6383-4f4b-a7a5-007ddd08f9ea/ewayb
https://ewbbackend-preprodpub-http.internal.cleartax.co/gst/v0.1/taxable_entities/1c74ddd2-6383-4f4b-a7a5-007ddd08f9ea/ewaybill/GLD23985?activity_type=GENERATE_EWB
标题:-
Content-type : application/json
X-Cleartax-Auth-Token : b1f57327-96db-4829-97cf-2f3a59a3a548
taxable_entity_id : b1f57327-96db-4829-97cf-2f3a59a3a548
正文:-
{
"id": "GLD23985",
"transaction_date": "26/10/2020",
"source": "USER",
"document_number": "BQ/20/0251",
"type": "OUTWARD",
"transport_mode": "ROAD",
"dispatch_from_state": "HARYANA",
"sub_supply": "Supply",
"distance": "90",
"vehicle_number": "TN32N1049",
"document_type": "Tax Invoice",
"seller": {
"address1": "142/1,Trunk Road",
"address2": "Perumugai",
"city": "Via Vellore",
"gstin": "29AEKPV7203E1Z9",
"name": "K.H Exports India Private Limited",
"state": "HARYANA",
"zip_code": ""
},
"receiver": {
"address1": "4/74, VOC Street, Seenerkuppam Village, ",
"address2": "Poonamalle, Chennai 600 056",
"city": "",
"gstin": "33AAACR1714R1ZA",
"name": "KH EXPORTS INDIA PVT.LTD. (LGD)",
"state": "TAMIL NADU",
"zip_code": "600003"
},
"consignee": {
"city": "",
"state": "TAMIL NADU",
"zip_code": "600003"
},
"line_items": [
{
"cess_rate": "0",
"cess_val": "0",
"cgst_rate": "0",
"cgst_val": "0",
"description": "STYLE;91311 COLOUR;SVFD7 BELT & PA",
"gst_code": "4203",
"igst_rate": "28.00",
"igst_val": "16800.000000",
"item_code": "STYLE;91311 COLOUR;SVFD7 BELT & PA",
"quantity": "3.00",
"serial_number": "1",
"sgst_rate": "0",
"sgst_val": "0",
"taxable_val": "600.0000",
"unit_of_measurement": "NUMBERS"
},
{
"cess_rate": "0",
"cess_val": "0",
"cgst_rate": "0",
"cgst_val": "0",
"description": "STYLE;91307 COLOUR;ABFD2 BELT & PA",
"gst_code": "4203",
"igst_rate": "28.00",
"igst_val": "16800.000000",
"item_code": "STYLE;91307 COLOUR;ABFD2 BELT & PA",
"quantity": "3.00",
"serial_number": "2",
"sgst_rate": "0",
"sgst_val": "0",
"taxable_val": "600.0000",
"unit_of_measurement": "NUMBERS"
}
]
}
答复:-
{
"errors": {
"err_1": {
"code": "BAD_REQUEST_ATTR",
"message": "Pincode should have 6 digits.",
"error_group_code": 0,
"error_id": 0,
"severity": "ERROR"
}
},
"error_sources": {
"seller": {
"zip_code": {
"error_refs": [
"err_1"
]
}
}
}
}
C#代码:-
嗨,我的朋友,你可以用这个代码来处理你的回复并得到你的答案。 前面的示例从控制器操作返回HttpResponseMessage消息,但您也可以使用HttpResponseException返回HttpError。这允许您在正常的成功案例中返回强类型模型,而在出现错误时仍然返回HttpError
public Product GetProduct(int id)
{
Product item = repository.Get(id);
if (item == null)
{
var message = string.Format("Product with id = {0} not found", id);
throw new HttpResponseException(
Request.CreateErrorResponse(HttpStatusCode.NotFound, message));
}
else
{
return item;
}
}
你收到控制器中的数据了吗?谢谢你的回复,实际上我正在从政府获取api,只是我正在执行这个api,所以请告诉我如何在C代码中控制这个错误
public Product GetProduct(int id)
{
Product item = repository.Get(id);
if (item == null)
{
var message = string.Format("Product with id = {0} not found", id);
throw new HttpResponseException(
Request.CreateErrorResponse(HttpStatusCode.NotFound, message));
}
else
{
return item;
}
}