【发布时间】:2014-04-17 21:50:21
【问题描述】:
有没有办法将路由参数添加到 ServiceStack 中的基本工厂路径?
目前,我们配置 IIS 以通过 web.config 将任何以/api/ 开头的请求路由到ServiceStackHttpHandlerFactory。
<location path="api">
<system.web>
<httpHandlers>
<add path="*" type="ServiceStack.WebHost.Endpoints.ServiceStackHttpHandlerFactory, ServiceStack" verb="*" />
</httpHandlers>
</system.web>
<!-- Required for IIS 7.0 -->
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<add path="*" name="ServiceStack.Factory" type="ServiceStack.WebHost.Endpoints.ServiceStackHttpHandlerFactory, ServiceStack" verb="*" preCondition="integratedMode" resourceType="Unspecified" allowPathInfo="true" />
</handlers>
</system.webServer>
此外,我们通过 AppHost.cs
中的端点主机配置告诉 ServiceStack 应用程序它有一个基本工厂路径api
SetConfig(new EndpointHostConfig { ServiceStackHandlerFactoryPath = "api" });
完成后,我们通过请求 DTO 上的 [Route] 属性指定到我们服务的路由,我们不必担心“/api”部分。
有没有办法给路径的路由添加一个全局参数?我们的想法是将这个参数添加到配置 /api/{Locale} 中,然后我们所有的请求 DTO 都将继承自 BaseRequest 具有属性 string Locale {get;set;} 的类。
作为额外的奖励,如果可以将其设为可选参数(可能通过web.config 和AppHost.cs 中的两条路由)以允许此参数的“默认”值会更好。
编辑
我目前正在探索创建一个扩展 RouteAttribute 类的类的想法。
public class PrefixedRouteAttribute : RouteAttribute
{
public static string Prefix = string.Empty;
public PrefixedRouteAttribute(string path) : base(Prefix + path)
public PrefixedRouteAttribute(string path, string verbs) : base(Prefix + path, verbs)
}
Prefix 值可以在AppHost.cs 中设置为/{Locale},如果我使用[PrefixedRoute] 而不是[Route],它将自动将Locale 参数注入所有路由。
这种方法似乎很有效,但我看不到使 {Locale} 成为可选的方法。
【问题讨论】:
标签: routing servicestack