简体   繁体   中英

Web-API Get with object

I created a Web-API and i would like to get all routes with parameters BeginAddress (string), EndAddress(string), BegineDate (Datetime). I created a new Class SearchRoute with these properties.

I can do a normal Getwith an id or a string but how to do a Get by giving an object? Is this possible?

Would it be possible to do a post/put with an object and than ask for a return?

using (HttpClient client = new HttpClient())
{
    HttpResponseMessage response = await client.GetAsync(url + userid);
    if (response.IsSuccessStatusCode)
    {
        string content = await response.Content.ReadAsStringAsync();
        List<Route> list = await SerializeService.Deserialize<List<Route>>(content);
        return list;
    }
    return null;
}

Web API Function

public List<Route> GetAllByCity(SearchRoute sr)
{
    return RouteDAO.GetAllByCity(sr);
}

Update: If i do this, the Post doesn't work but if i create a new controller it works.

[HttpPost]
// POST api/route
public void Post([FromBody]Route route)
{
    RouteDAO.Create(route);
}

// POST api/route
[HttpPost]
public List<Route> Post([FromBody]SearchRoute sr)
{
    return RouteDAO.GetAllByCity(sr);
}

I prefer sticking with GET even when using a complex object as a parameter. If you are concerned about the length of the URI then remember that:

  1. Prefixing the property names for simple like complex objects is not necessary because the Web API object binding can auto resolve based on property names alone.
  2. The maximum allowed URL length is 2083 characters which is more than sufficient in most cases.

If you we take your example

public class SearchRoute {
   public string BeginAddress {get;set;}
   public string EndAddress {get;set;}
   public DateTime BeginDate {get;set;}
}

[HttpGet]
public List<Route> Get([FromUri]SearchRoute sr)
{
    return RouteDAO.GetAllByCity(sr);
}

Uri when searching on

  • BeginAddress = "Some beginning";
  • EndAddress = "Some ending"
  • BeginDate = "2016-01-01T16:40:00"

Resulting query string:

?BeginAddress=Some beginning&EndAddress=Some ending&BeginDate=2016-01-01T16:40:00

Again, the properties will auto resolve even without the object prefix/qualifier and populate the object instance.

  • Add a domain info to the URL maybe another 50 or so characters
  • Add a controller name maybe another 30 or so characters
  • Add the query string = 82 characters

Note that I am not taking into account resolving the special characters like spaces to Url escaped character sequence

Total ≈ 162 characters give or take

Not bad considering that the maximum allowed URL length is 2083 characters, so you have used up only 7% of what is possible in this simple example.

This would probably be the preferred way of doing it because it conforms to the RESTful API standard where GET calls/verbs do not alter data and POST calls/verbs do.

You can pass an object by using a complex type in the URI. You need to help Web API by using the correctly formatted Query String. This would be an example:

?SearchRoute.BeginAddress=TheAddressValue&SearchRoute.EndAddress=TheAddressValue

However, if your Query String starts to become too big, you might be modeling the interaction incorrectly.

Then, in the server you should let Web API know that it should look in the URI for the values:

public List<Route> GetAllByCity([FromUri]SearchRoute sr)
    {
        return RouteDAO.GetAllByCity(sr);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM