繁体   English   中英

如何将 Object 作为 c# controller 中的字符串列表传递?

[英]How to pass Object in as string list in c# controller?

public class abcController : ApiController
{
    public IEnumerable<string> Get()
    {
        return new string[] { "A","B","C"};
    }

}

我想在字符串数组中传递包含名称、id 和图像的 object。如何做到这一点? 请帮助我.. 还告诉我如何访问此 object 中的任何特定元素..

我想这就是你要找的。 我已经把它放在控制台应用程序中。 您可以将逻辑复制到您的 controller

        public class ImageInformation
        {
            public ImageInformation(string name, string id, string image)
            {
                Name = name;
                Id = id;
                Image = image;
            }
            public string Name { get; private set; }
            public string Id { get; private set; }
            public string Image { get; private set; }
        }

        public static IEnumerable<ImageInformation> Get()
        {
            return new List<ImageInformation> { 
               new ImageInformation("A", "1", "A.jpg"),
               new ImageInformation("B", "2", "B.jpg"),
               new ImageInformation("C", "3", "C.jpg"),
            };
        }

        public static void Main()
        {
            var images = Get();
            var imageA = images.First(x => x.Name == "A");
            var imageAName = imageA.Name;
            var imageAId = imageA.Id;
            var imageAImage = imageA.Image;

            Console.WriteLine(imageAName);
            Console.WriteLine(imageAId);
            Console.WriteLine(imageAImage);
        }

您可以做的是使用 XML 序列化。

What XML Serializing does is it converts an object to XML format (which is a string) and then when you want to get the object back you can use XML Deserializer to Deserialize the XML string format to the original object.

例如,您的 object 可以是包含字符串名称、int ID、字符串 imagePath 的 class,我们称之为 class 人。

Class

[System.Serializable]
public class person
{
   public string name { get; set; }
   public int id { get; set; }
   public string imagePath { get; set; }
}

Class 备注

  • 确保 class 具有 system.Serializable 属性。
  • 确保您拥有的所有字段和属性都是公开的(否则程序将崩溃。)。

XML 管理器

  • 确保您具有以下命名空间,否则您将无法使用列出的任何方法
using System.Xml.Serialization;

XML_Manager class 我们将把我们的方法放在:-

public static class XML_Manager
    {
        // The Serializing Method
        public static string serializeObject<T>(T toSerialize)
        {
            // Create the result string variable
            string result = "";

            // Create a new StringWriter that will carry the serialization and dispose it as soon as it's done it's job
            using(StringWriter sw = new StringWriter())
            {
                // Create the serializer that will serialize the object of type (T)
                XmlSerializer xs = new XmlSerializer(typeof(T));

                // Serialize the object and put the result on the StringWriter (sw)
                xs.Serialize(sw, toSerialize);

                // Exctract the result from the StringWriter (sw) by converting it to string
                result = sw.ToString();
            }

            // Return the result
            return result;
        }

        // The Deserializing Method
        public static T deserializeObject<T>(string XmlString)
        {
            // Create the result variable of type (T)
            T Result;

            // Create a new StringReader that will carry the XmlString and dispose it as soon as it's done it's job
            using (StringReader sr = new StringReader(XmlString))
            {
                // Create the serializer that will deserialize the object of type (T)
                XmlSerializer xs = new XmlSerializer(typeof(T));

                // Deserialize the XmlString and extract the object
                Result = (T)xs.Deserialize(sr);
            }

            // Return the result
            return Result;
        }
    }

使脚本工作

            // Create a new person
            person p = new person();

            // Set the name
            p.name = "John";

            // Set the ID
            p.id = 2345;

            // Set the Image Path
            p.imagePath = "desktop";

            // the xml serialized string
            string xmlString = XML_Manager.serializeObject(p);

            // the deserialized person
            person deserializePerson = XML_Manager.deserializeObject<person>(xmlString);

            // Show the serialized string
            MessageBox.Show(xmlString);

            // Show the deserialized object
            MessageBox.Show($"Name: {deserializePerson.name}, Id: {deserializePerson.id}, Image Path: {deserializePerson.imagePath}");

如何通过列表<object>通过 MultipartFormDataContent c#<div id="text_translate"><p> 我试图了解如何使用 MultipartFormDataContent 进行 API 调用。 到目前为止,我已经能够发送带有字符串的变量和列表,但是每当我尝试发送带有整数的列表或带有类的列表时,API 都会检索到一个空列表。</p><p> 我该如何发送?</p><p> 这是我的有效代码:</p><pre> var multiForm = new MultipartFormDataContent(); var json = JsonConvert.SerializeObject(testClass.steps); //this is list of string with steps multiForm.Add(new StringContent(testEmployee.UserID.ToString()), "UserID"); multiForm.Add(new StringContent(json), "steps");</pre><p> 那么如何发送整数列表或员工列表? 我试过像上面那样做,但没有用。 然后 API 检索一个空列表。</p></div></object>

[英]How to pass List<object> via MultipartFormDataContent c#

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何传递C#中的列表对象中的值? 如何通过名单 <string> 到C#中的构造函数 如何将javascript对象传递给C#MVC 4控制器 如何将 integer 列表从 js 传递到 c# controller 如何在MVC C#中将列表作为参数传递给控制器 如何将对象转换为列表 <string> 在c#? 将整数列表传递给C#控制器操作 C# - Jquery:传递常规参数和列表 <KeyPairValue<string, string> &gt;使用$ .post从视图到控制器 如何通过列表<object>通过 MultipartFormDataContent c#<div id="text_translate"><p> 我试图了解如何使用 MultipartFormDataContent 进行 API 调用。 到目前为止,我已经能够发送带有字符串的变量和列表,但是每当我尝试发送带有整数的列表或带有类的列表时,API 都会检索到一个空列表。</p><p> 我该如何发送?</p><p> 这是我的有效代码:</p><pre> var multiForm = new MultipartFormDataContent(); var json = JsonConvert.SerializeObject(testClass.steps); //this is list of string with steps multiForm.Add(new StringContent(testEmployee.UserID.ToString()), "UserID"); multiForm.Add(new StringContent(json), "steps");</pre><p> 那么如何发送整数列表或员工列表? 我试过像上面那样做,但没有用。 然后 API 检索一个空列表。</p></div></object> 如何将列表对象传递给 Web API PUT 方法 C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM