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 ?

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

3 Comments

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.
See the update on how to bundle Condition, Sort and PageInfo into a single Json object that can be passed into the function.
I had the same solution in mind but I was looking for an easier way (if possible). Thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.