簡體   English   中英

如何獲取包含子類的API?

[英]How do I get my API to include a child class?

我有一個返回事件的Web API:

為簡潔起見,省略了不相關的部分

EventController.cs

namespace HobbsEventsMobile.Controllers
{
    public class EventController : ApiController
    {
        // GET api/event
        [HttpGet]
        public List<HobbsEventsMobile.Models.Event> Get()
        {
            return HobbsEventsMobile.Models.Event.GetEventSummary();
        }
}

這將調用:

Event.cs

namespace HobbsEventsMobile.Models
{
    public class Event
    {
        public int ID;
        public DateTime DateBegin;
        public DateTime DateEnd;
        public DateTime TimeBegin;
        public int Duration;
        public string Name;
        public string Description;
        public Category Category;
        public string Location;

        public Event()
        {

        }

        public static  List<Event> GetEventSummary()  //List<Event> GetEventSummary()
        {
            List<Event> events = new List<Event>();
            DataTable thisDT = new DataTable();
            using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
            {
                string SprocName = "HE_GetEventSummary";
                SqlDataAdapter thisAdapter = new SqlDataAdapter(SprocName, connection);
                thisAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
                thisAdapter.Fill(thisDT);
            }

            foreach(DataRow row in thisDT.Rows)
            {
                if (Convert.ToDateTime(row["EventDateBegin"]) >= DateTime.Today)
                {
                    Event myEvent = new Event();
                    myEvent.ID = Convert.ToInt32(row["EventID"]);
                    myEvent.DateBegin = Convert.ToDateTime(row["EventDateBegin"].ToString());
                    myEvent.DateEnd = Convert.ToDateTime(row["EventDateEnd"]);
                    myEvent.TimeBegin = Convert.ToDateTime(row["EventTimeBegin"]);
                    myEvent.Duration = Convert.ToInt32(row["Duration"]);
                    myEvent.Name = row["EventName"].ToString();
                    myEvent.Description = row["EventDesc"].ToString();
                    myEvent.Category = Category.Find(Convert.ToInt32(row["Category"]));
                    myEvent.Location = row["Location"].ToString();

                    events.Add(myEvent);
                }
            }

            return events;
        }
    }
}

其中有一個子班:

Category.cs

namespace HobbsEventsMobile.Models
{
    [DataContract]
    public class Category
    {
        public int ID;
        public string Name;

        public static List<Category> GetEventCategories()
        {
            List<Category> categories = new List<Category>();
            DataTable thisDT = new DataTable();
            using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
            {
                string SprocName = "HE_GetEventCategories";
                SqlDataAdapter thisAdapter = new SqlDataAdapter(SprocName, connection);
                thisAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
                thisAdapter.Fill(thisDT);
            }

            foreach (DataRow row in thisDT.Rows)
            {
                categories.Add(new Category(Convert.ToInt32(row["Category"]), row["CategoryName"].ToString()));
            }

            return categories;
        }

        public static Category Find(int id)
        {
            List<Category> categories = Category.GetEventCategories();
            return categories.First(c => c.ID == id);
        }

        public Category(int id, string name)
        {
            this.ID = id;
            this.Name = name;
        }
    }
}

這是問題所在:在將[DataContract]添加到Category ,我遇到了以下錯誤:

<ExceptionMessage>
    Type 'HobbsEventsMobile.Models.Category' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute. See the Microsoft .NET Framework documentation for other supported types.
</ExceptionMessage>

現在錯誤消失了,但是實際上並沒有返回任何類別:

<Event>
<Category/>
<DateBegin>2014-05-23T00:00:00</DateBegin>
<DateEnd>2014-05-23T00:00:00</DateEnd>
<Description>
Bring your blanket or lawn chairs and enjoy a free showing of Disney's Monster's University. Movie will begin at dusk with parking on the East side of the pool. 397-9291 for more information.
</Description>
<Duration>210</Duration>
<ID>2081</ID>
<Location>Del Norte Park/Pool</Location>
<Name>Movies Under the Stars</Name>
<TimeBegin>2014-05-23T19:30:00</TimeBegin>
</Event>

是什么賦予了?


更新1

要回答評論,是的,我已經進行了一些調試。 我為此創建了一個視圖:

從ApplicationController.cs

public ActionResult CategoryTest()
{
    ViewBag.Events = Event.GetEventSummary();
    return View();
}

CategoryTest.cshtml

<h2>CategoryTest</h2>
@{
    foreach (HobbsEventsMobile.Models.Event thisEvent in ViewBag.Events)
    {
        <li>@thisEvent.Name @thisEvent.Category.Name</li>
    }
}]

輸出:

在此處輸入圖片說明

在我的評論之外,我將舉一個例子。

我想你要:

namespace HobbsEventsMobile.Models
{
    [DataContract]
    public class Category
    {
        [DataMember]
        public int ID;
        [DataMember]
        public string Name;
        // everything else
    }
}

現在,當您序列化該類時,它現在也應該序列化變量。 正如您的代碼所示,它只是序列化了其中沒有任何內容的類。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM