gpt4 book ai didi

c# - REST API Azure 错误 : 'Microsoft.Rest.HttpOperationException' Operation returned an invalid status code 'Created'

转载 作者:行者123 更新时间:2023-12-03 01:08:03 28 4
gpt4 key购买 nike

当我尝试向我的 Azure REST API 应用程序发布帖子时,出现以下错误:

Exception: Exception caught: 'Microsoft.Rest.HttpOperationException' in Program.exe ("Operation returned an invalid status code 'Created'"). Exception caught: 'Microsoft.Rest.HttpOperationException' in Program.exe ("Operation returned an invalid status code 'Created'") 13,495.67s [7568] Worker Thread

它正确地发布到 API 应用程序(并将信息放置在 API 应用程序与之对话的数据库中),但由于某种原因,返回代码似乎未正确处理。当我将 REST API 客户端添加到我的解决方案中时,此帖子功能是自动生成的。为了解决这个问题,我只是将其嵌套在 try/catch 中,但我真的想解决这个问题,以便调试时标记的异常是合法的。

最佳答案

根据Microsoft.Rest.ClientRuntime.Azure查看CheckResponseStatusCodeFailed函数的源码,我们可以知道如果httpStatusCode.Created &&方法== HttpMethod.Put那么CheckResponseStatusCodeFaild函数将返回false。但在 httpStatusCode.Created && method == HttpMethod.Post 的情况下,则 CheckResponseStatusCodeFailed 等于 true。然后它会抛出异常。

private static bool CheckResponseStatusCodeFailed<TBody, THeader>(
AzureOperationResponse<TBody, THeader> initialResponse)
{
var statusCode = initialResponse.Response.StatusCode;
var method = initialResponse.Request.Method;
if (statusCode == HttpStatusCode.OK || statusCode == HttpStatusCode.Accepted ||
(statusCode == HttpStatusCode.Created && method == HttpMethod.Put) ||
(statusCode == HttpStatusCode.NoContent && (method == HttpMethod.Delete || method == HttpMethod.Post)))
{
return false;
}
return true;
}

在我的选择中,请尝试将 HttpMethod.Post 更改为 HttpMethod.Put 或添加 SwaggerResponse 属性 [SwaggerResponse(HttpStatusCode.OK)] 添加到您要为其指定有效 HTTP 响应代码的 Web API 操作方法。关于SwaggerResponse属性的更多信息,请引用customize expected response codes using the SwaggerResponse attribute .

关于c# - REST API Azure 错误 : 'Microsoft.Rest.HttpOperationException' Operation returned an invalid status code 'Created' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42634122/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com