我正在創建一個從 MySql 數據庫返回一些結果的 API。我有一個包含 2 行和一些字段的表。查詢的主要因素是一個名為Title. 應用程序應根據“標題”字段返回結果。在titles這兩個記錄是甜街和甜街2號。為了將結果返回為 JSON,我創建了一個類:using System.Collections.Generic;namespace IMES_Backend.Controllers{ internal class Movie { public class BaseResponse { public List<Item> search { get; set; } = new List<Item>(); public bool response { get; set; } } public class Item { public string title { get; set; } public string year { get; set; } public string released { get; set; } public string runTime { get; set; } public string genre { get; set; } public string director { get; set; } public string writer { get; set; } public string actors { get; set; } public string language { get; set; } public string country { get; set; } public string awards { get; set; } public string poster { get; set; } public string imdbScore { get; set; } public string production { get; set; } public string dl480p { get; set; } public string dl720p { get; set; } public string dl1080p { get; set; } public string subtitleLink { get; set; } public string dubLink { get; set; } public string description { get; set; } public string state { get; set; } } }}然后我選擇前面提到的 2 行并以 JSON 格式返回結果: [Route("IMES/api/GET/search/t={movieTitle}")] [HttpGet] public IActionResult MovieSearch(string movieTitle) { string searchKeyword = movieTitle.Replace("%20", " "); //Try to connect to the database try { } }當我瀏覽時.../IMES/api/GET/search/t=sweet,我只得到第一行數據,第二行沒有包含關鍵字的第二個列表sweet。我想在 JSON 的單獨列表中獲取兩行的數據。任何人都可以幫忙嗎?注意:我已經在 SQL Studio 中嘗試過查詢,我收到了兩條記錄。所以我確定那 2 行!
1 回答

呼如林
TA貢獻1798條經驗 獲得超3個贊
您正在從 while 循環返回
while(){
retun Ok(response);
}
,所以在添加第一項后返回。將其移出 while 循環
while(){
//do stuff
}
string response = JsonConvert.SerializeObject(baseResponse);
return Ok(response);
- 1 回答
- 0 關注
- 175 瀏覽
添加回答
舉報
0/150
提交
取消