C# 通过类传递带有响应代码的消息

C# 通过类传递带有响应代码的消息,c#,asp.net-web-api2,C#,Asp.net Web Api2,我使用的是一个web api,但我希望能够通过我的响应传回一条消息,但我在一个类的外部使用这个方法,在下面的示例中如何做到这一点 public HttpStatusCode CreateInvoice(string PumpName,string customerCode, double fuelQty, double price) { HttpStatusCode retval = new HttpStatusCode(); SAPbobsCOM.Documen

我使用的是一个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;           
    }
编辑2 好的,所以我创建了带有httprequest消息的函数,但是我没有在标题中看到结果,它显示了发票创建的状态200 Ok,但没有看到消息

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;
}
显示邮差结果

编辑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;
}
公共HttpResponseMessage CreateInvoice(字符串PumpName、字符串customerCode、双燃料数量、双价格、字符串燃料类型) { HttpResponseMessage retval=新的HttpResponseMessage(); sapbobcom.Documents oInvoice=company.GetBusinessObject(BoObjectTypes.oInvoices); HttpRequestMessage Errordescription=新建HttpRequestMessage(); oInvoice.DocDate=DateTime.Now; oInvoice.CardCode=客户代码; oInvoice.Lines.ItemCode=燃料类型; oInvoice.Lines.Quantity=燃料数量; oInvoice.Lines.LineTotal=价格; oInvoice.Lines.Add(); int addInvoice=oInvoice.Add(); 如果(addInvoice==0) { retval.StatusCode=HttpStatusCode.OK; retval.RequestMessage=新的HttpRequestMessage(HttpMethod.Post,“”); retval.Content=新的StringContent(“发票已创建!”); } 如果(附加值<0) { retval.StatusCode=HttpStatusCode.NotAcceptable; retval.Content=newStringContent(string.Format(“发票未创建{0}sap代码错误{1}!”,company.GetLastErrorDescription(),addInvoice)); } httpresponsemessageresponse=retval; 返回响应; }
将返回值设为元组怎么样

如果您使用的是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);
}
public(HttpStatusCode代码,字符串描述)CreateInvoice(字符串PumpName,字符串customerCode,双燃料数量,双价格)
{
HttpStatusCode retval=新的HttpStatusCode();
string errorDescription=string.Empty;
sapbobcom.Documents oInvoice=company.GetBusinessObject(BoObjectTypes.oInvoices);
oInvoice.DocDate=DateTime.Now;
oInvoice.CardCode=客户代码;
oInvoice.Lines.ItemCode=“DSL”;
oInvoice.Lines.Quantity=燃料数量;
oInvoice.Lines.LineTotal=价格;
oInvoice.Lines.Add();
int addInvoice=oInvoice.Add();
如果(addInvoice==0)
{
retval=HttpStatusCode.OK;
}
如果(附加值<0)
{
errorDescription=company.GetLastErrorDescription();
retval=HttpStatusCode.NotModified;
}
返回(代码:retval,说明:errorDescription);
}

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

将返回值设为元组如何

如果您使用的是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);
}
public(HttpStatusCode代码,字符串描述)CreateInvoice(字符串PumpName,字符串customerCode,双燃料数量,双价格)
{
HttpStatusCode retval=新的HttpStatusCode();
string errorDescription=string.Empty;
sapbobcom.Documents oInvoice=company.GetBusinessObject(BoObjectTypes.oInvoices);
oInvoice.DocDate=DateTime.Now;
oInvoice.CardCode=客户代码;
oInvoice.Lines.ItemCode=“DSL”;
oInvoice.Lines.Quantity=燃料数量;
oInvoice.Lines.LineTotal=价格;
oInvoice.Lines.Add();
int addInvoice=oInvoice.Add();
如果(addInvoice==0)
{
retval=HttpStatusCode.OK;
}
如果(附加值<0)
{
errorDescription=company.GetLastErrorDescription();
retval=HttpStatusCode.NotModified;
}
返回(代码:retval,说明:errorDescription);
}

如果旧版本需要返回元组,我强烈建议创建发票的函数不应该知道/关心http状态代码。
您只需要知道它是否创建了新发票,如果没有,为什么不创建。一种选择是使用异常,但如果“由于xxx原因未修改”是您期望经常发生的事情,那么这可能不是最好的解决方法。可以说,你想要的是某种有区别的联合,但C#没有一种很好的内置方式来实现这一点,因此你可以定义一种“响应”类型,它可以保存成功的响应或错误描述。然后在控制器层(需要知道http状态码等),根据响应对象的内容确定要返回给客户机的响应类型。如果您希望响应对象本身负责确定是否应该公开错误消息,那么可以有一个方法,该方法接受两个lambda,如果处于“成功”状态,则调用一个,如果处于失败状态,则调用另一个(将错误描述作为参数)。但这可能有些过分。

我强烈建议创建发票的函数不应该知道/关心http状态代码。
您只需要知道它是否创建了新发票,如果没有,为什么不创建。一种选择是使用异常,但如果“由于xxx原因未修改”是您期望经常发生的事情,那么这可能不是最好的解决方法。可以说,你想要的是某种有区别的联合,但C#没有一种很好的内置方式来实现这一点,因此你可以定义一种“响应”类型,它可以保存成功的响应或错误描述。然后在控制器层(需要知道http状态码等),根据响应对象的内容确定要返回给客户机的响应类型。如果您希望响应对象本身负责确定是否应该公开错误消息,那么可以有一个方法,该方法接受两个lambda,如果处于“成功”状态,则调用一个,如果处于失败状态,则调用另一个(将错误描述作为参数)。但这可以说是矫枉过正。

Create
out-string-errorDescriptionpublic (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);
}