簡體   English   中英

如何將HTTP狀態代碼從Web API中的方法返回給main?

[英]How do I return the HTTP Status Code from a method in my Web API to main?

我正在開發一個控制台應用程序,該應用程序可以從現有的Web API執行一些方法。 我希望控制台應用程序編寫返回的狀態代碼,但是我在努力尋找一種有效的語法。

例如,這是一種更新方法,目前正在運行,因為除了Web響應之外,我還希望它。

Console.WriteLine("Please enter a valid order item number for the specific data you want to update:");
                    string oNum = Console.ReadLine();

    //more prompts for user to enter data to update

    ...

var update = _service.UpdateOrder(oNum, oProc, oProcDate, oComplete);
Console.WriteLine("Status code: {0}", (update.StatusCode));

我了解為什么我當前的狀態代碼響應不起作用。 我已經嘗試了許多其他事情,但到目前為止我還是很困。 我想念什么?

編輯

這是_service.UpdateOrder引用的代碼:

public List<Stream> UpdateOrder(string orderID, bool processing, DateTime procDate, bool Complete)
    {
        var request = new RestRequest(StreamUrl, Method.PUT)
        {
            RequestFormat = DataFormat.Json
        };

        request.AddParameter("OrderID", orderID);
        ...


        var response = _client.Execute<List<Stream>>(request);
        if (response.StatusCode == System.Net.HttpStatusCode.Created || response.StatusCode == System.Net.HttpStatusCode.OK)
            return response.Data;
        else
            throw new Exception("Invalid input. Table could not be updated.");

您遇到的問題是您要從UpdateOrder方法返回stream

當前,您只能像現在這樣訪問UpdateOrder方法內的StatusCode屬性:

if (response.StatusCode == System.Net.HttpStatusCode.Created)

如果您要訪問StatusCode從調用代碼屬性,那么你需要返回IRestResponseUpdateOrder如下所示:

public IRestResponse UpdateOrder(string orderID, bool processing, DateTime procDate, bool Complete)
{
    var request = new RestRequest(StreamUrl, Method.PUT)
    {
        RequestFormat = DataFormat.Json
    };

    request.AddParameter("OrderID", orderID);
    ...


    var response = _client.Execute<List<Stream>>(request);
    if (response.StatusCode == System.Net.HttpStatusCode.Created || response.StatusCode == System.Net.HttpStatusCode.OK)
        return response;
    else
        throw new Exception("Invalid input. Table could not be updated.");
}

然后在您的調用代碼中:

Console.WriteLine("Please enter a valid order item number for the specific data you want to update:");
                    string oNum = Console.ReadLine();

    //more prompts for user to enter data to update

    ...

var update = _service.UpdateOrder(oNum, oProc, oProcDate, oComplete);
Console.WriteLine("Status code: {0}", (update.StatusCode));
// You can access the List<Stream> from update.Data

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM