Pass multiple complex objects to a post/put Web API method

后端 未结 11 1463
南旧
南旧 2020-11-29 21:51

Can some please help me to know how to pass multiple objects from a C# console app to Web API controller as shown below?

using (var httpClient = new System.N         


        
11条回答
  •  南笙
    南笙 (楼主)
    2020-11-29 22:23

    Here's another pattern that may be useful to you. It's for a Get but the same principle and code applies for a Post/Put but in reverse. It essentially works on the principle of converting objects down to this ObjectWrapper class which persists the Type's name to the other side:

    using Newtonsoft.Json;
    using System;
    using System.Collections.Generic;
    
    namespace WebAPI
    {
        public class ObjectWrapper
        {
            #region Public Properties
            public string RecordJson { get; set; }
            public string TypeFullName { get; set; }
            #endregion
    
            #region Constructors
    
            public ObjectWrapper() : this(null, null)
            {
            }
    
            public ObjectWrapper(object objectForWrapping) : this(objectForWrapping, null)
            {
            }
    
            public ObjectWrapper(object objectForWrapping, string typeFullName)
            {
                if (typeFullName == null && objectForWrapping != null)
                {
                    TypeFullName = objectForWrapping.GetType().FullName;
                }
                else
                {
                    TypeFullName = typeFullName;
                }
    
                RecordJson = JsonConvert.SerializeObject(objectForWrapping);
            }
            #endregion
    
            #region Public Methods
            public object ToObject()
            {
                var type = Type.GetType(TypeFullName);
                return JsonConvert.DeserializeObject(RecordJson, type);
            }
            #endregion
    
            #region Public Static Methods
            public static List WrapObjects(List records)
            {
                var retVal = new List();
                records.ForEach
                (item =>
                {
                    retVal.Add
                    (
                        new ObjectWrapper(item)
                    );
                }
                );
    
                return retVal;
            }
    
            public static List UnwrapObjects(IEnumerable objectWrappers)
            {
                var retVal = new List();
    
                foreach(var item in objectWrappers)
                {
                    retVal.Add
                    (
                        item.ToObject()
                    );
                }
    
                return retVal;
            }
            #endregion
        }
    }
    
    
    

    In the REST code:

    [HttpGet]
    public IEnumerable Get()
    {
        var records = new List();
        records.Add(new TestRecord1());
        records.Add(new TestRecord2());
        var wrappedObjects = ObjectWrapper.WrapObjects(records);
        return wrappedObjects;
    }
    
    
    

    This is the code on the client side (UWP) using a REST client library. The client library just uses the Newtonsoft Json serialization library - nothing fancy.

    private static async Task> Getobjects()
    {
        var result = await REST.Get>("http://localhost:50623/api/values");
        var wrappedObjects = (IEnumerable) result.Data;
        var unwrappedObjects =  ObjectWrapper.UnwrapObjects(wrappedObjects);
        return unwrappedObjects;
    }
    

    提交回复
    热议问题