Is it possible to pass more than one complex type parameter separately in the web API 2 Controller post method?
As it was mentioned by DavidG you could create any complex types that will suit your specific needs so for example
public class TranInOutContainer
{
public List<TranInOutDtl> TranInOutDtl Dtl {get; set;}
public List<TranInOutDtlsub> TranDtlSub DtlSub {get; set;}
....
}
will be valid solution for your problem
You could also use dynamic
type ofc but it should be used only if no other solution exists
How to receive multiple complex objects using HTTP POST method in Web App
You have "api/XYZ/" prefixed in your client code, but I don't see it on your server code. I predict that you have it in your server configuration but if not you will see issues.
You can only have one object with the [FromBody] tag in WebAPI, it's not clear how your trying to pass the
MyClass
object.Only simple objects, ints and strings, can be passed in the uri string so the
MyClass
object will not be transferred correctly.
I would recommend removing the MyClass
object or creating a larger object that encapsulates your List<SomeType> and
MyClass` and pass that in the body of the request with the [FromBody] decoration.
Here's more information on the topic
Posting a complex object with ASP.NET WebAPI
You can use Newtonsoft.Json which will help you serialize your data to a json object. It can be used like this
using Newtonsoft.Json;
public static void SaveOrUpdateEntity(string url, object data)
{
var dataString = JsonConvert.SerializeObject(data);
using (var client = new WebClient())
{
client.Headers.Add(HttpRequestHeader.ContentType, "application/json");
response = client.UploadString(new Uri(url), "POST", dataString);
}
}
To learn more about the newtonsoft library, read here
Passing multiple parameters to web API GET method
You can pass a custom model to an action method, but I suggest not using the GET
for you task because GET
does not have a body.
Instead, use the SEARCH
verb and put the list of serials number and the date inside a custom model in the body.
public class MeterSearchModel
{
public List<string> Serials {get;set;}
public DateTime Date {get;set;}
}
In .NET Core 2 your controller would have something like -
[AcceptVerbs("SEARCH")]
public async Task<IActionResult> Search([FromBody] MeterSearchModel model)
{
//..perform search
}
Can the Post for a ODataContoller in WebAPI 2 take an IEnumerable of a complex object
Technically this is possible. If you get null
as complexObjects in the following call
[HttpPost]
public IHttpActionResult CreateMany([FromBody] IEnumerable<ComplexObject> complexObjects)
{
// ...
}
it's probably due to a format error in the bodies json object. If you have a working single object you can post, you just need to wrap it in brackets ... an array is an array even if it cotains only one element. This assumes, you check your web api actions via postman, fiddler etc., where you can 'compose' the entire request. Alternatively you can use the output of a 'GET all' action (if you have one) as input.
Regarding another aspect, the REST-fulness of list creation, you may find RESTful way to create multiple items in one request interesting
Hope this helps.
Related Topics
Drawing on Top of Controls Inside a Panel (C# Winforms)
Is It Safe for Structs to Implement Interfaces
JSON Serializer Object with Internal Properties
Datetime.Adddays() Not Working as Expected
Use Different Name for Serializing and Deserializing with JSON.Net
Nullable Object Must Have a Value
How to Test Your Request.Querystring[] Variables
Execute Unit Tests Serially (Rather Than in Parallel)
In-Memory Database Doesn't Save Data
Print Rdlc Report Without Showing Reportviewer Control
Why Does the Is Operator Return False When Given Null
Intersect with a Custom Iequalitycomparer Using Linq
Including Pictures in an Outlook Email
Get the Object Is Null Using JSON in Wcf Service
Using Moq to Mock an Asynchronous Method for a Unit Test
Curious Null-Coalescing Operator Custom Implicit Conversion Behaviour