1

I have a simple controller method like this:

public IEnumerable<IEntity> GetEntities(ParamsModel args)
{
   //set break point here to examine the args
   return null;
}

And here is my ParamsModel:

public class ParamsModel {
   public string Test;
}

And here is my client's method to send get request:

//_client here is an instance of RestClient
public async Task<IEnumerable<T>> GetEntitiesAsync()
{
   var request = new RestRequest("somePath");
   var o = new {                
            Test = "OK"
           };
   request.AddJsonBody(o);       
   return await _client.GetAsync<List<T>>(request);       
}

After running the method GetEntitiesAsync, the break point (in the controller's method) is hit. However the args is null, really?

I've also tried the following:

public async Task<IEnumerable<T>> GetEntitiesAsync()
{
   var request = new RestRequest("somePath");
   request.AddParameter("Test", "OK");
   return await _client.GetAsync<List<T>>(request);       
}

However that did not work as well (args is null in the controller's method). If I change the controller's method to something like this (and use the client code as right above), I can see the single simple argument of string has value parsed OK ("OK") inside the controller's method:

public IEnumerable<IEntity> GetEntities(string Test)
{
   //here we can see that Test has value of "OK"
   return null;
}

Really I don't understand what's wrong with my code. Actually I worked with RestSharp at least a year ago but now it seems to have some new methods (such as the GetAsync as I used in my code), as before I used the Execute and ExecuteAsync.

Could you spot anything wrong here? Thanks!

PS: I'm using RestSharp 106.6.7

3
  • HTTP GET requests do not allow a body and as such the server wont read it. Commented Jan 25, 2019 at 12:06
  • the AddParameter will add it as a query string parameter. Commented Jan 25, 2019 at 12:07
  • @Nkosi thanks, so the AddParameter will be added to body? what should I do instead in this case (to help parse data to ParamsModel expectedly)? thanks. Commented Jan 25, 2019 at 12:07

1 Answer 1

2

Update action to state explicitly where to look for and bind data using [FromUri]

public IHttpActionResult GetEntities([FromUri]ParamsModel args) {

    //...

    return Ok(entities);
}

To force Web API to read a complex type from the URI, add the [FromUri] attribute to the parameter.

Reference Parameter Binding in ASP.NET Web API

The example with AddParameter

public async Task<IEnumerable<T>> GetEntitiesAsync() {
   var request = new RestRequest("somePath");
   request.AddParameter("Test", "OK");
   return await _client.GetAsync<List<T>>(request);       
}

Should work now.

Note that the model should use properties instead of fields

public class ParamsModel {
   public string Test { get; set; }
}
Sign up to request clarification or add additional context in comments.

3 Comments

OK, it worked now. Thank you. Do you think that this kind of design (by using [FromUri] in this case is just really unnecessary? I mean that in this case the method is obviously GET, so its parameters can only be obtained from URI (as you said the body will not be read by server). So why do I have to instruct it to read data from URI in this case? isn't that so ridiculous ?
That is just how the framework was designed. I would suggest you review the link in the answer to get a better understanding of model binding.
yes I know that's by design, but actually it is not like the model binding in MVC (and I'm familiar with MVC before web api). At least in this case the model binding in MVC works in the way making much more sense than Web API.

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.