基本上我有以下架构:
依赖关系:
在我的Api项目中,我定义了ServiceStack.Webhost.Endpoints.AppHostBase
的具体实现,例如ApiAppHost
:
public sealed class ApiAppHost : AppHostBase
{
private ApiAppHost()
: base("Description", typeof (ApiAppHost).Assembly) {}
public override void Configure(Container container)
{
this.SetConfig(new EndpointHostConfig
{
ServiceStackHandlerFactoryPath = "api"
});
this.Routes.Add<Foo>("/foo", "POST");
}
public static void Initialize()
{
var instance = new ApiAppHost();
instance.Init();
}
}
这非常简单。
现在,我想从我的网站项目中查询我的this.Routes
(与EndpointHostConfig.ServiceStackHandlerFactoryPath
结合使用),以获取 Foo
的特定路径。
如果不自行创建拦截器,我怎么能这样做呢? ServiceStack.Net是否提供适合的任何内容?
答案 0 :(得分:1)
目前我正在做这样的事情
public static class AppHostBaseExtensions
{
public static string GetUrl<TRequest>(this AppHostBase appHostBase)
{
var requestType = typeof (TRequest);
return appHostBase.GetUrl(requestType);
}
public static string GetUrl(this AppHostBase appHostBase, Type requestType)
{
var endpointHostConfig = appHostBase.Config;
var serviceStackHandlerFactoryPath = endpointHostConfig.ServiceStackHandlerFactoryPath;
var serviceRoutes = appHostBase.Routes as ServiceRoutes;
if (serviceRoutes == null)
{
throw new NotSupportedException("Property Routes of AppHostBase is not of type ServiceStack.ServiceHost.ServiceRoutes");
}
var restPaths = serviceRoutes.RestPaths;
var restPath = restPaths.FirstOrDefault(arg => arg.RequestType == requestType);
if (restPath == null)
{
return null;
}
var path = restPath.Path;
var virtualPath = "~/" + string.Concat(serviceStackHandlerFactoryPath, path); // bad, i know, but combining with 2 virtual paths ...
var absolutePath = VirtualPathUtility.ToAbsolute(virtualPath);
return absolutePath;
}
}
我知道这是错误的,因为许多问题(路径组合,不考虑休息路径,不考虑占位符的休息路径),但它起作为一个开始......
编辑:
这只适用于您在Configure(Container)
实施的AppHostBase
内注册路线的情况。它不适用于RestServiceAttribute
- 属性......