2014-11-21 176 views
1

嗨,我是新來的servicestack 有問題,與路由 我有相匹配的路線Servicestack路由問題

[Route("/Person/{ID}", "GET")] 
public class GetPersonByID : IReturn<PersonResponse> 
{ 
    public decimal ObjectId { get; set; } 
} 
[Route("/Organization/{ID}/Person", "GET")] 
public class GetPersonByOrganizationId : List<PersonResponse> 
{ 
    public decimal ObjectId { get; set; } 
} 

但後來我試圖/Organization/281478302400588/Persons,我得到一個錯誤說 Unable to bind request

堆棧跟蹤:在ServiceStack.Host.RestHandler.CreateRequest(IRequest httpReq, IRestPath restPath)在 ServiceStack.Host.RestHandler.ProcessRequestAsync(IRequest httpReq, IResponse httpRes,字符串operationName)

回答

1
  • 你需要確保的是,在路線段名的DTO的屬性相匹配。所以{ID}應該{ObjectId}

  • 在第二個請求,你應該使用IReturn<List<PersonResponse>>,而不是你的要求從List<PersonResponse>繼承

[Route("/Person/{ObjectId}", "GET")] 
public class GetPersonByID : IReturn<PersonResponse> 
{ 
    public decimal ObjectId { get; set; } 
} 

[Route("/Organization/{ObjectId}/Person", "GET")] 
public class GetPersonByOrganizationId : IReturn<List<PersonResponse>> 
{ 
    public decimal ObjectId { get; set; } 
} 
  • 你也注意到你正在努力/Organization/281478302400588/Persons已使用請求中有Persons,但路由爲Person,因此請相應地更改請求或路由。 (可能是最好的途徑,即[Route("/Organization/{ObjectId}/Persons", "GET")]

然後在你的服務確保您設置它與此類似:。

public class PersonService : Service 
{ 
    public PersonResponse Get(GetPersonByID request) 
    { 
     // return new PersonResponse(); 
    } 

    public List<PersonResponse> Get(GetPersonByOrganizationId request) 
    { 
     // return new List<PersonResponse>(); 
    } 
} 

我希望幫助

+1

它的工作就像一個魅力 – 2014-11-25 23:10:29

+0

@KennethFoliJørgensen太棒了,很高興幫助 – Scott 2014-11-25 23:11:58