[英]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.