我正在使用C#API返回Contest数据。我有一个List并将其作为API的输出返回。
API网址:API Url
在此输出中,List按字母顺序排序。我想在下面提到的Contest类顺序中使用List,而不进行排序。
比赛课程:
public class Contest
{
public int ContestID { get; set; }
public string MovieName { get; set; }
public string MovieImage { get; set; }
public string RegionalMovieName{get;set;}
public string Question { get; set; }
public string Answer1 { get; set; }
public string Answer2 { get; set; }
public string Answer3 { get; set; }
public string Ad1ImageUrl { get; set; }
public string Ad2ImageUrl { get; set; }
public string Ad3ImageUrl { get; set; }
public string AdText { get; set; }
public DateTime? StartDate { get; set; }
public DateTime? EndDate { get; set; }
}
在输出列表中使用上述类。
控制器代码:
public class ContestController : ApiController
{
MMProvider.ContestProvider Provider = new MMProvider.ContestProvider();
[HttpGet]
[Route("GetContestDetails")]
public IHttpActionResult GetContestDetail(string ContestId)
{
Contest _contest = new Contest();
if (!string.IsNullOrEmpty(ContestId.ToString()))
{
List<Contest> ContestDetailsList = Provider.GetContestDetail(ContestId);
return Ok(ContestDetailsList);
}
else
{
return NotFound();
}
}
}
StoredProcedure的:
ALTER PROCEDURE [dbo].[Proc_GetMblContest]
@ContestID INT
AS
BEGIN
IF EXISTS (SELECT COUNT(ContestID) FROM Mbl_Contest WHERE ContestID = @ContestID AND Status='A')
BEGIN
SELECT ContestID,MovieName,RegionalLanguage,MovieImage,Ad1ImageUrl,Ad2ImageUrl,Ad3ImageUrl,AdText,Question,Answer1,Answer2,Answer3,StartDate,EndDate FROM Mbl_Contest WHERE ContestID = @ContestID
END
END
答案 0 :(得分:2)
没有&#34;班级顺序&#34;。类成员存在没有任何内部订单,无论您输入什么顺序。
假设您使用的是default serializers for WebApi,则需要指定所需的顺序:
[DataContract]
public class Contest
{
[DataMember(Order = 0)]
public int ContestID { get; set; }
[DataMember(Order = 1)]
public string MovieName { get; set; }
[DataMember(Order = 2)]
public string MovieImage { get; set; }
[DataMember(Order = 3)]
public string RegionalMovieName{get;set;}
// ... the pattern should be clear
}