I've been assigned to develop the WebAPI controller for an application (something I had never worked with before). Everything went fine, had some basic requests like GetAllUsers(int id) just for testing reasons - the configuration itself is fine.
Now here's the issue. I have a method
GetAllItems(Carrier carrier)
where Carrier is a class with a number of different parameters. As we already have a few Carrier instances in the database for testing purposes, what I've tried was querying the database, selecting the instance of Carrier based on the ID (GUID) attribute, but to no result.
Is there a way to test GET requests when the input parameter is an object, rather than a single value (such as int ID, for example) manually, with a test method or a test input parameter of some sort ?
EDIT.: Thanks everyone for the feedback, the solution to my issue was actually much easier to fix than I had expected. I would absolutely love to upvote all of you, although unfortunately my reputation is too low to do so (I'm new to stackoverflow), so I'll have to get back to doing so at some point in the near future. Cheers :)
It clearly states Can't bind multiple parameters. This indicates that we cannot pass multiple entities as an input parameter to Web API action methods.
Show activity on this post. MakeUser method in the User controller for creating a username and password. UserParameter class for sending the parameters as an object. RunAsync method in console client.
You can pass parameters to Web API controller methods using either the [FromBody] or the [FromUri] attributes. Note that the [FromBody] attribute can be used only once in the parameter list of a method.
Please note that we are able to send [FromBody] parameter in HTTP GET Request input.
As far as i understand your question, you want to be able to pass the Carrier's properties directly in the URL rather than in your request body.
ex:
[GET] http://localhost/entities?id=000000000000000
You controller method is this one
GetAllItems(Carrier carrier)
Carrier has a Id (Guid) property :
class Carrier {
public Guid Id { get; set; }
public string Name { get; set; }
}
Carrier is a complex object in term of WebApi model binding.
Default behavior for model binding is :
By default, Web API uses the following rules to bind parameters: If the parameter is a “simple” type, Web API tries to get the value from the URI. Simple types include the .NET primitive types (int, bool, double, and so forth), plus TimeSpan, DateTime, Guid, decimal, and string, plus any type with a type converter that can convert from a string. (More about type converters later.) For complex types, Web API tries to read the value from the message body, using a media-type formatter.
see: http://www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api
Expecting a model binding with a complex object in the URL is not the WebApi default behavior.
If you want your controller method to model-bind a complex object from the URL you have to tell it.
GetAllItems([FromUri] Carrier carrier)
With the FromUri binding indicator, you can use the complex model binding from the URL
Now you can even add more properties mapping in the URL :
[GET] http://localhost/entities?id=000000000000000&name=ABC
GetAllItems will received a Carrier object populated with : carrier.Id = 0000-00000000000-000; carrier.Name = "ABC"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With