// #################################################################################### 【Login2】
[HttpPost]
public HttpResponseMessage LoginApi([FromBody]Body model)
{
// 创建一个对象来保存要返回的数据
var result = new { username = "csj", password = "123" };
// 序列化对象为 JSON 格式
string jsonResult = JsonConvert.SerializeObject(result);
// 创建一个 HttpResponseMessage 对象,并设置其 Content 为 JSON 格式的字符串
var response = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(jsonResult, System.Text.Encoding.UTF8, "application/json")
};
return response;
}
二、另一种形式
// #################################################################################### 【Login2】
[HttpPost]
public HttpResponseMessage LoginApi([FromBody]Body model)
{
LogManager.WriteLog("========LoginApi - 888 ===========");
//string Sql_Body = model.BodyValue; // 【表名】
//string Sql_Table = Json_Read.Json_KeyValueArray(Sql_Body, "username"); // 【表名】 "Time_Task"
//LogManager.WriteLog(Sql_Table);
string jsonString = @"
{
""code"": 0,
""message"": ""ok"",
""data"": {
""token"": ""eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiLmtYvor5Xmiafms5XkurrlkZgiLCJleHAiOjE3MjQ2Njc2NjIsImlhdCI6MTcyNDY2MDQ2Mn0.r_cBg4YvklKKRAMj5FtAcRzKcYI4iIgAAlrULL_7SvU""
}
}";
// 将 jsonString 包装成 HttpResponseMessage
var response = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(jsonString, System.Text.Encoding.UTF8, "application/json")
};
return response;
}