1

I am working on a legacy system that has been using .Net remoting for its communication.Now I want to write new client and server side web-api code to do the same thing. Here's a sample code that I am dealing with :

public SearchResult Search(Condition condition,Sort sort,PageInfo pageInfo)
{
......
}

I would like to be able to have a web-api action with the same signature that gets its value form Uri , thus :

[HttpGet()]    
public SearchResult Search([FromUri]Condition condition,[FromUri]Sort sort,[FromUri]PageInfo pageInfo)
        {
        ......
        }

Here are my questions :

  1. Is it possible to have such an action in a web-api ?
  2. If yes, How can I pass these parameters using HttpClient ?
Beatles1692
  • 5,214
  • 34
  • 65

1 Answers1

3

You can setup your Route attribute to accept as many parameters as you like.

[Route("/api/search/{condition}/{sort}/{pageInfo}")]
[HttpGet]
public HttpResponseMessage Search( string condition, string sort, string pageInfo) {
    ...
}

This would mean that your url changes from
/Search?condition=test&sort=first&pageInfo=5
to
/Search/test/first/5

Alternatively, bundle the Condition, Sort and PageInfo objects into single Json class, and pass this object to your route:

[Route("/api/search/{SortParams}")]
[HttpGet]
public HttpResponseMessage Search( object sortParams) {
    // deserialize sortParams into C# object structure
}

Have a look at this question : C# .net how to deserialize complex object of JSON

Community
  • 1
  • 1
blorkfish
  • 21,800
  • 4
  • 33
  • 24
  • I know but it means that I should write hundred of routes for hundred methods and there's a possibility that in future some parameters will be added to these methods thus we should change the route every time a new parameter is added.One other thing to note : Condition , Sort and PageInfo are objects not strings. – Beatles1692 May 15 '14 at 07:56
  • See the update on how to bundle Condition, Sort and PageInfo into a single Json object that can be passed into the function. – blorkfish May 15 '14 at 08:10
  • I had the same solution in mind but I was looking for an easier way (if possible). Thanks :) – Beatles1692 May 15 '14 at 08:26