c# - RestClient - 请求返回 500 内部服务器错误(内容为 html)并且没有 ErrorMessage

标签 c# asp.net json response restsharp

我在使用 REST 客户端的 PUT 请求时遇到问题。我在 Postman 中尝试了同样的方法,我得到了一个 ErrorResponse 并返回了正确的错误消息。所以我在 C# - RestSharp Request 调用中期望相同。相反,我得到一个 500 Internal Server Error,request.ErrorException = null 和 request.ErrorMessage = null。在有人指责我 Request header 之前,我确实设置了 request.AddHeader("Accept", "application/json");

我的代码是这样的

var client = new RestClient(uri);
var request = new RestRequest(Method.PUT);
request.AddHeader("Accept", "application/json");`
request.AddParameter("xx...", value); 
var response = client.Execute(request);
var content = response.Content;   

所以现在我看到的response.Content是下面的html(我只发布部分内容以供引用)

<html>
    <head>
        <title>Runtime Error</title>
        <meta name="viewport" content="width=device-width" />
        <style>
         body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;} 
         p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px}
         b {font-family:"Verdana";font-weight:bold;color:black;margin-top: -5px}
         H1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red }
         H2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon }
         pre {font-family:"Consolas","Lucida Console",Monospace;font-size:11pt;margin:0;padding:0.5em;line-height:14pt}
         .marker {font-weight: bold; color: black;text-decoration: none;}
         .version {color: gray;}
         .error {margin-bottom: 10px;}
         .expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; }
         @media screen and (max-width: 639px) {
          pre { width: 440px; overflow: auto; white-space: pre-wrap; word-wrap: break-word; }
         }
         @media screen and (max-width: 479px) {
          pre { width: 280px; }
         }
        </style>
    </head>

    <body bgcolor="white">
.....

....
</html>

现在在我的包装器方法中,我正在检查 StatusCode 是否不是 200,然后如果 request.ErrorException 为 null,在这种情况下只显示一条通用消息“出了点问题......”类型一个东西。这只是目前的创可贴解决方案。我想知道如何获得准确的错误响应或如何处理这种情况。

非常感谢任何帮助。

最佳答案

在 Web 服务中,除非您明确地将错误消息设置为其属性之一,否则将永远不会返回实际错误。例如,在 WebAPI 中,您可以使用 Request.CreateResponse() 方法返回错误消息。该方法需要返回 HttpResponseMessage 类型。以下是代码

[HttpPut]
public HttpResponseMessage MyPutMethod()
{
try
{
...process
return Request.CreateResponse(HttpStatusCode.OK, "Success");
}
catch (Exception ex)
{
return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
}
}

注意:出于安全原因,发送异常详细信息不是一个好主意。

关于c# - RestClient - 请求返回 500 内部服务器错误(内容为 html)并且没有 ErrorMessage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41348464/

相关文章:

c# - 服务结构访问 unc 驱动器以存储文件。是否可以?

c# - 我必须做什么才能访问我自己的公共(public)方法?

asp.net - 防止对动态创建的 DOM 网页和动态生成的 javascript 事件处理程序进行 XSS 攻击

c# - 为什么 .net 核心依赖注入(inject)对我不起作用?

json - 如何使用 postgres 在 jsonb 数据中找到最小值?

c# - 客户端发现响应内容类型为 'text/html' ,但预期为 'text/xml'

c# - .net 4.5.1 不支持在代码后面写入 "System.AppContext"吗?

c# - 同一页上有多个图像的 Crystal 报表

javascript - Vue JS 在 console.log 上返回 [Object object] 并在 JSON.parse 上返回未定义

c++ - 使用 jsoncpp 将 json 数据增量写入文件