【问题标题】:Adding Parameters to ServiceStack's base path将参数添加到 ServiceStack 的基本路径
【发布时间】: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.configAppHost.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


    【解决方案1】:

    这应该可行:

    public abstract class BaseDto
    {
        public string Locale { get; set; }
    
        public BaseDto()
        {
            Locale = "en";
        }
    }
    
    [Route("/{Locale*}/test")]
    public class TestDto : BaseDto
    {
        public int SomeParam { get; set; }
    }
    
    public class TestService : Service
    {
        public Result Get(TestDto request)
        {
            ...
        }
    }
    

    【讨论】:

    • 感谢您回复阿米尔。我不再从事该项目,因此无法验证您的回复。但是,由于您是唯一给出任何答案的人,并且可以想象这看起来应该可行,所以我将其标记为正确。
    猜你喜欢
    • 1970-01-01
    • 2014-04-18
    • 2020-08-16
    • 2020-03-11
    • 2013-12-02
    • 2013-11-16
    • 1970-01-01
    • 2020-06-24
    • 2010-11-17
    相关资源
    最近更新 更多