Я использую веб-API, но я хочу иметь возможность передать сообщение с моим ответом, но я использую этот метод вне класса, как это можно сделать в следующем примере.
public HttpStatusCode CreateInvoice(string PumpName,string customerCode, double fuelQty, double price)
{
HttpStatusCode retval = new HttpStatusCode();
SAPbobsCOM.Documents oInvoice = company.GetBusinessObject(BoObjectTypes.oInvoices);
oInvoice.DocDate = DateTime.Now;
oInvoice.CardCode = customerCode;
oInvoice.Lines.ItemCode = "DSL";
oInvoice.Lines.Quantity = fuelQty;
oInvoice.Lines.LineTotal = price;
oInvoice.Lines.Add();
int addInvoice = oInvoice.Add();
if (addInvoice == 0)
{
retval = HttpStatusCode.OK;
}
if (addInvoice < 0)
{
string errorDescription = company.GetLastErrorDescription();
retval = HttpStatusCode.NotModified;
}
return retval;
}
Я хочу иметь возможность передать эту строку обратно как часть ответного сообщения. Я не знаю, как это сделать в контроллере, но эта функция находится вне класса.Как там у меня нет доступа к объекту запроса?
string errorDescription = company.GetLastErrorDescription();
Редактировать 2 Хорошо, поэтому я создал функцию с сообщением httprequest, но я не вижу результат в заголовке, который показывает мнестатус 200 в порядке для счета, созданного, но не для сообщения.
public HttpResponseMessage CreateInvoice(string PumpName,string customerCode, double fuelQty, double price,string FuelType)
{
HttpResponseMessage retval = new HttpResponseMessage();
SAPbobsCOM.Documents oInvoice = company.GetBusinessObject(BoObjectTypes.oInvoices);
HttpRequestMessage Errordescription = new HttpRequestMessage() ;
oInvoice.DocDate = DateTime.Now;
oInvoice.CardCode = customerCode;
oInvoice.Lines.ItemCode = FuelType;
oInvoice.Lines.Quantity = fuelQty;
oInvoice.Lines.LineTotal = price;
oInvoice.Lines.Add();
int addInvoice = oInvoice.Add();
if (addInvoice == 0)
{
retval.StatusCode = HttpStatusCode.OK;
retval.RequestMessage=new HttpRequestMessage(HttpMethod.Post, "Invoice has been created!");
}
if (addInvoice < 0)
{
retval.StatusCode = HttpStatusCode.NotAcceptable;
retval.RequestMessage = new HttpRequestMessage(HttpMethod.Post,string.Format("Invoice was not created {0} sap code error {1}!", company.GetLastErrorDescription(),addInvoice));
}
HttpResponseMessage response = retval;
return response;
}
Вот как я использую сообщение в моем контроллере API.
public HttpResponseMessage Post(string PumpName, string FuelTagNumber,
double FuelQty, double FuelValue, string FuelType, string TransactionDate, string TransActionDate, string systemgroup1, string systemgroup2, string systemgroup3, string systemgroup4)
{
HttpResponseMessage retVal = new HttpResponseMessage();
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, "value");
int connect = _boneAPi.Connect();
if (connect == 0)
{
string CustomerCode = _boneAPi.GetCustomerCodeByVechicleTag(FuelTagNumber);
HttpResponseMessage _invoiceStatusCode = _boneAPi.CreateInvoice(PumpName, CustomerCode, FuelQty, FuelValue,FuelType);
retVal = _invoiceStatusCode;
_boneAPi.ImportTransactionToTable("", CustomerCode, TransactionDate, TransactionDate, systemgroup1, systemgroup3, FuelTagNumber, systemgroup2, systemgroup4, FuelQty.ToString(), FuelValue.ToString(), FuelType, "1");
}
return retVal;
}
Чтобы отобразить результат сообщения пользователя
Редактировать 2
Чтобы показать другим, как я это решил.
public HttpResponseMessage CreateInvoice(string PumpName, string customerCode, double fuelQty, double price, string FuelType)
{
HttpResponseMessage retval = new HttpResponseMessage();
SAPbobsCOM.Documents oInvoice = company.GetBusinessObject(BoObjectTypes.oInvoices);
HttpRequestMessage Errordescription = new HttpRequestMessage();
oInvoice.DocDate = DateTime.Now;
oInvoice.CardCode = customerCode;
oInvoice.Lines.ItemCode = FuelType;
oInvoice.Lines.Quantity = fuelQty;
oInvoice.Lines.LineTotal = price;
oInvoice.Lines.Add();
int addInvoice = oInvoice.Add();
if (addInvoice == 0)
{
retval.StatusCode = HttpStatusCode.OK;
retval.RequestMessage = new HttpRequestMessage(HttpMethod.Post, "");
retval.Content = new StringContent("Invoice has been created!");
}
if (addInvoice < 0)
{
retval.StatusCode = HttpStatusCode.NotAcceptable;
retval.Content = new StringContent(string.Format("Invoice was not created {0} sap code error {1}!", company.GetLastErrorDescription(), addInvoice));
}
HttpResponseMessage response = retval;
return response;
}