使用 RestSharp 返回一个简单的 Guid
Returning a simple Guid with RestSharp
RestSharp 无法反序列化普通 Guid 的响应。
[Route("User/BeginSession")]
[HttpGet]
public Guid BeginSession()
{
return Guid.NewGuid();
}
此测试失败:
[Fact]
public void BeginSession_MadeAsARestRequest_Succeeds()
{
var client = new RestClient("http://localhost/xyz");
var request = new RestRequest("User/BeginSession");
var a = client.Execute(request);
var result = client.Execute<Guid>(request).Data;
Assert.NotEqual(
Guid.Parse("{00000000-0000-0000-0000-000000000000}"),
result);
}
我必须用 class 包装 Guid 才能让它工作:
public class GuidResponse
{
public Guid Guid { get; set; }
}
[Route("User/BeginSession2")]
[HttpGet]
public GuidResponse BeginSession2()
{
return new GuidResponse
{
Guid = Guid.NewGuid()
};
}
此测试通过:
[Fact]
public void BeginSession2_MadeAsARestRequest_Succeeds()
{
var client = new RestClient("http://localhost/xyz");
var request = new RestRequest("User/BeginSession2");
var result = client.Execute<GuidResponse>(request).Data;
Assert.NotEqual(
Guid.Parse("{00000000-0000-0000-0000-000000000000}"),
result.Guid);
}
这是预期的行为吗?
我想这是因为只返回一个 GUID 是无效的 JSON。它将作为字符串返回,而不是作为 JavaScript 对象返回,例如{ "id": "4bae9421-4fe0-4294-a659-9bc37388344b" }
RestSharp 无法反序列化普通 Guid 的响应。
[Route("User/BeginSession")]
[HttpGet]
public Guid BeginSession()
{
return Guid.NewGuid();
}
此测试失败:
[Fact]
public void BeginSession_MadeAsARestRequest_Succeeds()
{
var client = new RestClient("http://localhost/xyz");
var request = new RestRequest("User/BeginSession");
var a = client.Execute(request);
var result = client.Execute<Guid>(request).Data;
Assert.NotEqual(
Guid.Parse("{00000000-0000-0000-0000-000000000000}"),
result);
}
我必须用 class 包装 Guid 才能让它工作:
public class GuidResponse
{
public Guid Guid { get; set; }
}
[Route("User/BeginSession2")]
[HttpGet]
public GuidResponse BeginSession2()
{
return new GuidResponse
{
Guid = Guid.NewGuid()
};
}
此测试通过:
[Fact]
public void BeginSession2_MadeAsARestRequest_Succeeds()
{
var client = new RestClient("http://localhost/xyz");
var request = new RestRequest("User/BeginSession2");
var result = client.Execute<GuidResponse>(request).Data;
Assert.NotEqual(
Guid.Parse("{00000000-0000-0000-0000-000000000000}"),
result.Guid);
}
这是预期的行为吗?
我想这是因为只返回一个 GUID 是无效的 JSON。它将作为字符串返回,而不是作为 JavaScript 对象返回,例如{ "id": "4bae9421-4fe0-4294-a659-9bc37388344b" }