当前控制器返回指定数据类型
/// <summary>
/// 控制器
/// </summary>
public class LoginController : Controller
{
//创建私有变量
private readonly IUser _user;
//创建构建函数,通过传参的方式 注入依赖
public LoginController(IUser user)
{
_user = user;
}
// 返回一个页面:这个页面是用来展示登录的页面
public IActionResult Index()
{
return View();
}
/// <summary>
/// 登录
/// </summary>
/// <param name="userName"></param>
/// <param name="userPwd"></param>
/// <returns></returns>
[HttpPost] // 提交登录-一开始登录的时候,需要去处理的业务逻辑
public IActionResult SignIn(string userName, string userPwd)
{
List<User> userInfos = _user.GetUserAll(userName, userPwd);
if (userInfos.Count == 0)
{
return Ok(new ApiResult
{
Code = 0,
Msg = "登录失败",
Data = new
{
userName,
userPwd
}
});
}
else
{
return Ok(new ApiResult
{
Code = 1,
Msg = "登录成功",
Data = new
{
userName,
userPwd
}
});
}
}
/// <summary>
/// 返回指定数据类型
/// </summary>
class ApiResult
{
public int Code { get; set; } = 0;
public string Msg { get; set; }
public object Data { get; set; }
}
}