通过类将消息传递回带有响应代码的消息

时间:2018-10-25 08:16:49

标签: c# asp.net-web-api2

我正在使用Web 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;

 }

显示邮递员结果

enter image description here

编辑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;
}

2 个答案:

答案 0 :(得分:3)

如何使返回元组

如果您使用的是c#7,它将如下所示:

public (HttpStatusCode code, string description) CreateInvoice(string PumpName, string customerCode, double fuelQty, double price)
{
    HttpStatusCode retval = new HttpStatusCode();
    string errorDescription = string.Empty;

    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)
    {
        errorDescription = company.GetLastErrorDescription();
        retval = HttpStatusCode.NotModified;
    }

    return (code: retval, description: errorDescription);
}

如果是旧版本,则需要返回一个元组

答案 1 :(得分:2)

我强烈建议创建发票的功能完全不应该知道/关心http状态代码。 您需要知道的是它是否创建了新发票,如果不是,则为什么不创建。一种选择是使用异常,但是如果您希望合理地经常发生“由于原因xxx而未被修改”,那可能不是最佳的解决方法。可以说,您想要的是某种可区分的并集,但是C#并没有很好的实现此目的的方式,因此您可以定义一个“响应”类型,该类型可以包含成功的响应或错误描述。然后在您的控制器层(确实需要了解http状态代码等)中,根据响应对象的内容确定返回给客户端的响应类型。如果您希望响应对象本身负责确定是否应公开错误消息,则可以有一个采用两个lambda的方法,如果一个处于“成功”状态则调用一个,或者调用另一个(带有错误说明)作为参数)处于失败状态。但这可以说是矫kill过正。