【问题标题】:ServiceStack: Handler for request not found?ServiceStack:未找到请求的处理程序?
【发布时间】:2013-04-09 02:18:54
【问题描述】:

我定义了三个路线。前两个工作正常,但最后一个在主题中返回错误。

    Routes.Add<FeeInstructionsList>("/policies/{clientPolicyId}/feeinstructions", "GET");
    Routes.Add<FeeInstructionsEdit>("/policies/{clientPolicyId}/feeinstructions/{feetype}", "GET");
    Routes.Add<List<FeeInstructionEditInfo>>("/policies{clientPolicyId}/feeinstructions", "POST");

当我有第三条路线只是“/feeinstructions”时,它工作正常,但是当我像上面那样添加路线时它不起作用。

FeeInstructionEditInfo 没有名为“clientPolicyId”的成员。这可能是一个原因。如果是这样,我如何将它传递给服务而不需要修改我的 dto。它可以是这样的服务操作的附加参数吗?我不认为这是可能的,因为 ss 是每个请求一个 dto,但也许有办法?

public List<FeeInstructionEditInfo> Post(int clientPolicyId, List<FeeInstructionEditInfo> request)

目前这个方法被声明为

public List<FeeInstructionEditInfo> Post(List<FeeInstructionEditInfo> request)

这是正在发送的请求

> POST http://localhost:12543/api/policies/680455600/feeinstructions/
> HTTP/1.1 Content-Type: application/json Accept-Language: en-US
> Referer: http://localhost:12543/ClientBin/SR.SRUnite.ShellUI.xap
> Content-Length: 1408 Accept-Encoding: identity Accept:
> application/json User-Agent: Mozilla/5.0 (compatible; MSIE 9.0;
> Windows NT 6.1; WOW64; Trident/5.0; BOIE9;ENUS) Host: localhost:12543
> Connection: Keep-Alive Pragma: no-cache

这些是我原来的 dto,并没有改变

 [Route("/policies/{clientPolicyId}/feeinstructions/{feetype}","GET")]
 public class FeeInstructionsEdit
 {
     public int ClientPolicyId { get; set; }
     public string FeeType { get; set; }
 }

 public class FeeInstructionsEditResponse
 { 
     public List<KeyValuePair> Instructions { get; set; }
     public List<KeyValuePair> Contacts { get; set; }
     public List<FeeInstructionEditInfo> FeeInstructions { get; set; }       
 }

public partial class FeeInstructionEditInfo
{
    public int FeeInstructionId { get; set; }

    public string FeeTypeCode { get; set; }
    public string FeeTypeDescription { get; set; }
    public string FeeTypeGroupCode { get; set; }

    public string ProductName { get; set; }
    public string InsuredType { get; set; }
    public string Demographic { get; set; }
    public string Instruction { get; set; }
    public string Contact { get; set; }
    public decimal? FeeAmount { get; set; }
}

我会将 FeeInstructionEditInfo 列表发布到 /clientPolicies/342434/feeinstructions 并且它不起作用,但发布到 /feeinstructions 会。

我现在使用这对 dto 发布。

 [Route("feeinstructions","POST")]
 public class FeeInstructionsSave
 {
     public int ClientPolicyId { get; set; }
     public List<FeeInstructionEditInfo> FeeInstructions { get; set; }
 }

 public class FeeInstructionsSaveResponse : IHasResponseStatus
 {
      public ResponseStatus ResponseStatus { get; set; }
 }

【问题讨论】:

标签: servicestack


【解决方案1】:

ServiceStack 中的fundamental concept每个服务 都需要使用Request DTO 来调用。可以使用 PathInfo、QueryString 和请求正文的任意组合填充此请求 DTO。

这意味着如果你想传入一个集合,你需要让你的 Request DTO 继承它,例如:

[Route("/policies/{clientPolicyId}/feeinstructions", "POST")]
public class EditFeeInstructions : List<FeeInstructionEditInfo>
{
}

现在一切正常:

public List<FeeInstructionEditInfo> Post(EditFeeInstructions request)
{
   ...
}

对于空请求,您的服务将如下所示:

public class EmptyRequest {} 

public object Post(EmptyRequest request)
{
   ...
}

即使你想process the request manually yourself,你仍然需要提供一个Request DTO来表明你的意图,例如:

public class Hello : IRequiresRequestStream
{
    //The raw Http Request Input Stream gets injected here
    public Stream RequestStream { get; set; }
}

【讨论】:

  • 感谢您的回答,但我想我可能没有给您足够的信息来回答。我理解这个基本概念。我用我最初的方式和现在的方式更新了我的问题。
  • 路由组件必须用/. 分隔符分隔,您不能像尝试那样进行部分单词匹配。请参阅Routing 上的文档了解更多信息。
  • @mytz:哈,那是一个错字,应该有“/”,我没有尝试部分匹配。谢谢。
猜你喜欢
  • 1970-01-01
  • 2021-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-19
  • 1970-01-01
  • 2013-07-02
  • 2015-04-22
相关资源
最近更新 更多