簡體   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