C#Restsharp-如何验证内容结果是意外结果

本文关键字:结果是 意外 结果 验证 何验证 C#Restsharp- | 更新日期: 2023-09-27 18:00:13

获取内容结果的代码

IRestResponse response = client.Execute(request);
var content = (string)JsonConvert.DeserializeObject(response.Content);

response.content应该返回这4个消息中的一个。

  • 好的
  • 无效消息ID
  • 消息NOT_FOUND
  • 内部错误

如果API返回其他内容,我该如何验证?

C#Restsharp-如何验证内容结果是意外结果

检查内容:

if(content != "OK" && content != "INVALID_MESSAGE_ID" && content != "MESSAGE_NOT_FOUND" && content != "INTERNAL_ERROR") {
    //Do some handling, like logging
}

或者:

switch(content) {
    case "OK":
        // Do something, when content is OK
        break;
    case "INVALID_MESSAGE_ID":
        // Do something, when content is INVALID_MESSAGE_ID
        break;
    case "MESSAGE_NOT_FOUND":
        // Do something, when content is MESSAGE_NOT_FOUND
        break;
    case "INTERNAL_ERROR":
        // Do something, when content is INTERNAL_ERROR
        break;
    case default:
        // Do something, if no case is matched
        break;
}