[英]Grapevine Rest Server Route Pathinfo with dynamic values
我是C#的新手,需要實現REST服務,所以我偶然發現了Grapevine。 我需要讓服務的URL的一部分通過配置文件在服務啟動時移交給我,但我不能將配置文件的值“ clientId”移交給路由的Pathinfo,因為它不是恆定的。 這是代碼的一部分:
[RestResource(BasePath = "/RestService/")]
public class Rest_Resource
{
public string clientId = ConfigurationManager.AppSettings["ClientId"];
[RestRoute(PathInfo = clientId + "/info")]//<-how do I fill Pathinfo with dynamic values?
public IHttpContext GetVersion(IHttpContext context)
{....}
}
我在Visual Studio中使用vinevine v4.1.1作為nuget包。
盡管可以在運行時更改屬性值 ,甚至使用動態屬性 ,但在這種情況下,更簡單的解決方案可能是不獨占使用自動發現功能,而使用混合方法進行路由注冊。
考慮以下包含兩個休息路線的類,但是其中只有一個裝飾有屬性:
[RestResource(BasePath = "/RestService/")]
public class MyRestResources
{
public IHttpContext ManuallyRegisterMe(IHttpContext context)
{
return context;
}
[RestRoute(PathInfo = "/autodiscover")]
public IHttpContext AutoDiscoverMe(IHttpContext context)
{
return context;
}
}
由於您要使用直到運行時才知道的值注冊第一條路由,因此我們可以手動注冊該路由:
// Get the runtime value
var clientId = "someValue";
// Get the method info
var mi = typeof(MyRestResources).GetMethod("ManuallyRegisterMe");
// Create the route
var route = new Route(mi, $"/RestService/{clientId}");
// Register the route
server.Router.Register(route);
這需要手動注冊需要運行時值的路由,但是我們仍然希望自動發現其他路由。 由於路由器僅在服務器啟動時路由表為空時才會自動發現,因此我們必須告訴路由器何時掃描程序集。 您可以在手動注冊路由之前或之后執行此操作:
server.Router.ScanAssemblies();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.