【问题标题】:Routing based on query string parameter name in servicestack?基于服务堆栈中的查询字符串参数名称的路由?
【发布时间】:2015-10-07 09:55:05
【问题描述】:

我想根据我的查询字符串参数路由两个不同的 RequestDTO 类。 PFB 两种情况。

Scenario 1:- 

localhost:8080/myservice?type="abc" //Service URL

// should be called below DTO class

public class TypeRequestDTO //DTO
{
   public string type;
}

Scenario 2:- 
--------------------------------------------------
localhost:8080/myservice?Name="xyz" //Service URL

// should be called below DTO class

public class NameRequestDTO //DTO
{
   public string Name;
}

在这两种情况下,base url 相同 (/myservice) 但我想根据querystring parameter 进行路由。有没有办法做到这一点。

【问题讨论】:

    标签: servicestack


    【解决方案1】:

    您不能通过 queryString 进行路由,它只是用于填充由同一服务处理的请求 DTO 的属性。

    但在服务内部,您可以委托给不同的服务,例如:

    [Route("/myservice")]
    public class Request
    {
        public string Type { get; set; }
    }
    
    public class MyService : Service
    {
        public object Any(Request request)
        {
            if (request.Type == "abc")
               return Any(request.ConvertTo<AbcRequest>());
    
           return Any(request.ConvertTo<XyzRequest>());
        }
    
        public object Any(AbcRequest request) { ... }
    
        public object Any(XyzRequest request) { ... }
    }
    

    如果实现在不同的服务类中,您可以使用:

    return base.ExecuteRequest(request.ConvertTo<AbcRequest>());
    

    这是从 IOC 解析 Autowired Service 并执行它的简写,例如:

    using (var service = base.ResolveService<AbcService>())
    {
        return service.Any(request.ConvertTo<AbcRequest>());
    }
    

    【讨论】:

      猜你喜欢
      • 2015-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-07
      • 1970-01-01
      • 2013-08-03
      相关资源
      最近更新 更多