簡體   English   中英

C# 發布到 asp.net webapi Apiexception“響應的 HTTP 狀態代碼未被排除 (201)。”

[英]C# post to asp.net webapi Apiexception "The HTTP status code of the response was not excepted (201)."

我希望你能在這里幫助我。

我正在創建一個 C# 應用程序,它讀取日志文件並將特定條目發送到 Webapi。 到目前為止,它並不漂亮,需要進行一些大修,但它或多或少地工作了。

該程序使用多個等待指令,因為日志文件生成條目的速度如此之快,以至於 http 發布速度不夠快;)

在我的第一種方法中,我讀取了日志文件並將一些符合我的條件的行發送到另一種方法,以使用此語句將其刪除

Task<string> marketstring = MarketstringAsync(s);
                                await marketstring;

在 Marketstring 方法中,我用我需要的信息在不同部分切斷線路,並將其發送到我的 api 方法:

Task<string> MarketSendApi = MarketSendApiAsync(order);
                string apiresult = await MarketSendApi;

現在我們在 apisend 方法中。 我在 asp.net 核心團隊的教程視頻之后創建了它,並測試了一些東西以使其運行。

private static async Task<string> MarketSendApiAsync(string sendtoapi)
        {
            Console.WriteLine("api");
            //Console.WriteLine(sendtoapi);

            var httpClient = new HttpClient();
            var client = new swaggerClient("https://localhost:7250/", httpClient);
            //var results = await client.MarketDatumAsync(sendtoapi);
            //string res = results.Id;
            
            //return results.Playername;

            System.Collections.Generic.ICollection<MarketDatum> marketData = (System.Collections.Generic.ICollection<MarketDatum>)await client.MarketDatumAsync(sendtoapi);
            //string apiresult = marketData;


            //if (marketData == null)
            //{
            //    return "error";
            //};

            //return marketData;
            //return new StatusCodeResult(await client.MarketDatumAsync(sendtoapi)
            //return await client.MarketDatumAsync(sendtoapi);
            return "test";

        }

帖子工作正常,數據在我的 webapi 上創建,但現在程序“崩潰”,在 await 市場字符串出現以下異常; 第一種方法

market_api.ApiException:“響應的 HTTP 狀態代碼不是預期的 (201)。

狀態:201 響應:“myData”

我的 api 方法如下所示:

public ActionResult<MarketDatum> Set([FromBody] string value)
        {
            if (value == "")
            {
                return BadRequest();
            }
            using (var context = new CoreDbContext())
            {
                //add new Orders
                MarketDatum orders = new MarketDatum();
    .....
    return CreatedAtAction(nameof(GetbyID), new { id = orders.Id }, orders);

我希望我以一種可以理解的方式解釋了我的問題,並且您找到了我的問題,謝謝;)

太澄清一些,在哪一行拋出異常?

我認為引發錯誤是因為您使用的是 CreatedAtAction。 看這里: https://docs.microsoft.com/en-us/aspnet/core/web-api/action-return-types?view=aspnetcore-6.0

暫無
暫無

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

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