如何生成指向 ServiceStack 中定义的特定服务的 URL?
我想在响应 DTO 中包含到其他端点的完整或相对 URL。RestServiceBase
contains RequestContext.AbsoluteUri
,但这完全取决于请求。
如何生成指向 ServiceStack 中定义的特定服务的 URL?
我想在响应 DTO 中包含到其他端点的完整或相对 URL。RestServiceBase
contains RequestContext.AbsoluteUri
,但这完全取决于请求。
wiki 中的反向路由部分展示了如何在填充的请求 DTO上使用扩展方法来生成相对和绝对 URI:
如果您使用[Route]
元数据属性(而不是 Fluent API),您将能够仅使用 DTO 生成强类型 URI,让您在 ServiceStack Web 框架之外创建 url,就像使用 .NET 服务客户端使用ToUrl(HttpMethod)
and一样ToAbsoluteUri(HttpMethod)
,例如:
[Route("/reqstars/search", "GET")]
[Route("/reqstars/aged/{Age}")]
public class SearchReqstars : IReturn<ReqstarsResponse>
{
public int? Age { get; set; }
}
var relativeUrl = new SearchReqstars { Age = 20 }.ToGetUrl();
var absoluteUrl = new SearchReqstars { Age = 20 }.ToAbsoluteUri();
relativeUrl.Print(); //= /reqstars/aged/20
absoluteUrl.Print(); //= http://www.myhost.com/reqstars/aged/20
电子邮件联系人演示展示了一个使用上述反向路由扩展方法为Razor 视图中的 HTML 表单和链接填充路由的示例。
new RequestDto().ToPostUrl();
new RequestDto().ToPutUrl();
new RequestDto().ToDeleteUrl();
new RequestDto().ToOneWayUrl();
new RequestDto().ToReplyUrl();
您还可以使用以下命令检查传入的底层 httpRequest:
var httpReq = base.RequestContext.Get<IHttpRequest>();
以及底层的 ASP.NET(或 HttpListener)请求对象:
var aspNetReq = httpReq.OriginalRequest;
它们应该包含应该更有用的附加属性。