gpt4 book ai didi

servicestack - 向路由中给定的路径发送 GET 请求

转载 作者:行者123 更新时间:2023-12-04 17:23:32 24 4
gpt4 key购买 nike

我正在尝试从这样的 URL 调用 REST 服务:

example.org/account/someusername

我已经定义了请求和响应 DTO。
[Route("/account/{UserName}", "GET")]
public class AccountRequest : IReturn<AccountResponse>
{
public string UserName { get; set; }
}

public class AccountResponse
{
public int Id { get; set; }
public string UserName { get; set; }
public string Bio { get; set; }
}

调用服务:
JsonServiceClient client = new JsonServiceClient("http://example.org");
AccountRequest request = new AccountRequest { UserName = "me" };

AccountResponse response = client.Get(request);

但是,当我在客户端上调用 Get 时,它不尊重路由。当我在调试器中检查客户端实例时,AsyncOneWayBaseUri 值为 example.org/json/asynconeway/。这部分无关紧要,因为这并不意味着将请求发送到此 URL。我实际上不知道它在哪里发送请求。我没有收到任何错误,响应对象中的所有属性都为空。

我在这里缺少什么?

最佳答案

使用第 3 方 REST/HTTP API

ServiceStack 的服务客户端固执地调用 ServiceStack Web 服务,因为它们支持 ServiceStack 的预定义路由、内置身份验证、自动路由生成、内置错误处理等。

要调用第 3 方 REST/HTTP API,您可以使用 HTTP Utils ServiceStack.Text 提供了简洁、易读的 API,用于围绕 .NET 的常见数据访问模式 HttpWebRequest ,例如:

List<GithubRepo> repos = "https://api.github.com/users/{0}/repos".Fmt(user)
.GetJsonFromUrl()
.FromJson<List<GithubRepo>>();

使用 C# .NET 服务客户端使用 ServiceStack 服务

我没有看到报告的行为,您是否在客户端上使用最新版本的 ServiceStack?

测试使用的生成 url 的一种方法(不进行服务调用)是调用 TRequest.ToUrl(method)直接扩展方法(服务客户端使用),例如
AccountRequest request = new AccountRequest { UserName = "me" };
request.ToUrl("GET").Print(); // /account/me

当我尝试通过 JsonServiceClient 调用它时,使用了相同的自动生成的路由。 ,例如:
var client = new JsonServiceClient("http://example.org");
var response = client.Get(request); //calls http://example.org/account/me

ServiceStack 的服务客户端中使用的路由 URL

ServiceStack 将尝试使用最合适的路由来匹配您正在调用的 DTO 和 HTTP 方法中填充的值,如果没有匹配的路由,它将回退到 pre-defined routes .

默认情况下,将使用原始预定义路由:
/api/[xml|json|html|jsv|csv]/[syncreply|asynconeway]/[servicename]

但 ServiceStack 现在也支持较短的别名 /reply/oneway ,例如:
/api/[xml|json|html|jsv|csv]/[reply|oneway]/[servicename]

您可以通过设置标志来选择在客户端中使用:
client.UseNewPredefinedRoutes = true;

关于servicestack - 向路由中给定的路径发送 GET 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15647173/

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