简体   繁体   中英

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

i hope you can help me here.

I am creating an C# application which reads an logfile and send specific entries to an Webapi. So far its not pretty and it need a few overhauls but it worked more or less.

The Programm uses several wait instructions because the logfile generates entries so fast that the http Post isnt fast enough;)

In my first method i read the logfile and send some lines which matches my criteria to another method to cut it with this statement

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

In the Marketstring method i cut the line in different part with the informations that i need and send it to my api method with this:

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

and now we are in the apisend method. I created it after an tutorial video from asp.net core team and tested a few things to get it running.

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";

        }

The post works fine and the data get created on my webapi but now the Programm "crashes" with the following exception at the await marketstring; the first method with

market_api.ApiException: "The HTTP status code of the response was not expected (201).

Status: 201 Response: "myData"

my api method looks like this:

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);

i hope i explained my problem in an understandable way and you find my Problem Thanks;)

Too clarify some, on which line is the exception thrown?

I think the error is thrown cause you are using CreatedAtAction. Look through here: https://docs.microsoft.com/en-us/aspnet/core/web-api/action-return-types?view=aspnetcore-6.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM