public class ApiResponse<T>
{
public bool Success { get; set; }
public T? Data { get; set; }
public string? Message { get; set; }
public ApiResponse(bool success, T? data, string errorMessage = "")
{
Success = success;
Data = data;
Message = errorMessage;
}
}
//引入数据库上下文
private readonly everydaytodoContext _context;
public TestController(everydaytodoContext context)
{
_context = context;//实例化数据库上下文
}
[HttpGet("Hello")]
public async Task<IActionResult> GetTest()
{
var result = await _context.Test.Where(x => x.Name == "11").ToListAsync();//LINQ 语法
//throw new Exception("测试异常");
//return new JsonResult(new { code = 200, msg = "获取成功", data = result });
// 封装响应结果
var response = new ApiResponse<List<Models.Test>>(true, result);
return Ok(response);
}
}
返回格式如截图