简体   繁体   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)."

i hope you can help me here.我希望你能在这里帮助我。

I am creating an C# application which reads an logfile and send specific entries to an Webapi.我正在创建一个 C# 应用程序,它读取日志文件并将特定条目发送到 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;)该程序使用多个等待指令,因为日志文件生成条目的速度如此之快,以至于 http 发布速度不够快;)

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:在 Marketstring 方法中,我用我需要的信息在不同部分切断线路,并将其发送到我的 api 方法:

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

and now we are in the apisend method.现在我们在 apisend 方法中。 I created it after an tutorial video from asp.net core team and tested a few things to get it running.我在 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";

        }

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;帖子工作正常,数据在我的 webapi 上创建,但现在程序“崩溃”,在 await 市场字符串出现以下异常; the first method with第一种方法

market_api.ApiException: "The HTTP status code of the response was not expected (201). market_api.ApiException:“响应的 HTTP 状态代码不是预期的 (201)。

Status: 201 Response: "myData"状态:201 响应:“myData”

my api method looks like this:我的 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);

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.我认为引发错误是因为您使用的是 CreatedAtAction。 Look through here: https://docs.microsoft.com/en-us/aspnet/core/web-api/action-return-types?view=aspnetcore-6.0看这里: 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.

相关问题 在ASP.NET WebApi中更改HTTP响应 - Change HTTP response in ASP.NET WebApi AngularJS使用Asp.net Web API:$ http post返回XMLHttpRequest无法加载:预检的响应具有无效的HTTP状态代码405 - AngularJS With Asp.net Web API: $http post returning XMLHttpRequest cannot load: Response for preflight has invalid HTTP status code 405 如何使用ASP.Net/C#读取客户端URL的HTTP标头状态代码 - How to read HTTP-Header status code of client URL by using ASP.Net/C# HTTP状态码200,但访问被拒绝。 WebAPI ASP.NET MVC - HTTP status code 200 but access denied. WebAPI ASP.NET MVC Twilio与C#ASP.NET WebAPI - Twilio with c# asp.net webapi 在C#asp.net中接收和使用HTTP Post JSON - receive and use HTTP Post JSON in C# asp.net HTTP CODE 405(不允许的方法),当我在 ASP.NET WebAPI CORS 中发送 POST 数据时 - HTTP CODE 405 (Method Not Allowed), When I sent POST data in ASP.NET WebAPI CORS 返回JSON作为ASP.NET WebApi控制器中HTTP Get的响应 - Return JSON as a response for HTTP Get in ASP.NET WebApi Controller 在ASP.Net WebAPI控制器中返回完整的HTTP响应 - Return full HTTP Response in ASP.Net WebAPI Controller C#/。NET webapi-从具有列表返回类型的控制器中的方法返回异常(HTTP状态代码) - C#/.NET webapi - Returning an exception (HTTP Status Code) From Method in Controller with List Return Type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM