Skip to main content
An small correction in the sample code.
Source Link
Tohid
  • 6.7k
  • 8
  • 58
  • 83

I have the following Web API (GET):

public class UsersController : ApiController { public IEnumerable<Users> Get(string firstName, string LastName, DateTime birthDate) { // Code } } 

It's a GET, so I can call it like this:

http://localhost/api/users?firstName=john&LastName=smith&birthDate=1979/01/01 

and receive an xml result of user(s).

Is it possible to encapsulate parameters to one class like this:

public class MyApiParameters { public string FirstName {get; set;} public string LastName {get; set;} public string DateTime BirthDate {get; set;} } 

And then have:

 public IEnumerable<Users> Get(MyApiParameters parameters) 

I've tried it and anytime I try to get result from http://localhost/api/users?firstName=john&LastName=smith&birthDate=1979/01/01, the parameter is null.

I have the following Web API (GET):

public class UsersController : ApiController { public IEnumerable<Users> Get(string firstName, string LastName, DateTime birthDate) { // Code } } 

It's a GET, so I can call it like this:

http://localhost/api/users?firstName=john&LastName=smith&birthDate=1979/01/01 

and receive an xml result of user(s).

Is it possible to encapsulate parameters to one class like this:

public class MyApiParameters { public string FirstName {get; set;} public string LastName {get; set;} public string DateTime {get; set;} } 

And then have:

 public IEnumerable<Users> Get(MyApiParameters parameters) 

I've tried it and anytime I try to get result from http://localhost/api/users?firstName=john&LastName=smith&birthDate=1979/01/01, the parameter is null.

I have the following Web API (GET):

public class UsersController : ApiController { public IEnumerable<Users> Get(string firstName, string LastName, DateTime birthDate) { // Code } } 

It's a GET, so I can call it like this:

http://localhost/api/users?firstName=john&LastName=smith&birthDate=1979/01/01 

and receive an xml result of user(s).

Is it possible to encapsulate parameters to one class like this:

public class MyApiParameters { public string FirstName {get; set;} public string LastName {get; set;} public DateTime BirthDate {get; set;} } 

And then have:

 public IEnumerable<Users> Get(MyApiParameters parameters) 

I've tried it and anytime I try to get result from http://localhost/api/users?firstName=john&LastName=smith&birthDate=1979/01/01, the parameter is null.

Source Link
Tohid
  • 6.7k
  • 8
  • 58
  • 83

How to develop an ASP.NET Web API to accept a complex object as parameter?

I have the following Web API (GET):

public class UsersController : ApiController { public IEnumerable<Users> Get(string firstName, string LastName, DateTime birthDate) { // Code } } 

It's a GET, so I can call it like this:

http://localhost/api/users?firstName=john&LastName=smith&birthDate=1979/01/01 

and receive an xml result of user(s).

Is it possible to encapsulate parameters to one class like this:

public class MyApiParameters { public string FirstName {get; set;} public string LastName {get; set;} public string DateTime {get; set;} } 

And then have:

 public IEnumerable<Users> Get(MyApiParameters parameters) 

I've tried it and anytime I try to get result from http://localhost/api/users?firstName=john&LastName=smith&birthDate=1979/01/01, the parameter is null.