You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.3 KiB

2 years ago
using AspNetCoreMVC.DataContext;
using AspNetCoreMVC.Models;
2 years ago
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
namespace AspNetCoreMVC.Controllers
{
[Route("api/user")] // 컨트롤러 전체 라우터 설정
public class UserController : Controller
{
[HttpGet("{id}")]
public JsonResult GetUser(int id)
{
2 years ago
var db = new ModelDbContext();
var user = db.Users.SingleOrDefault(u => u.Id == id);
2 years ago
return Json(user);
}
[HttpGet("v2/{id}")]
public IActionResult GetUser2(int id)
{
2 years ago
var db = new ModelDbContext();
var user = db.Users.SingleOrDefault(u => u.Id == id);
2 years ago
if (user == null)
return BadRequest("No data found");
return Ok(user);
}
[HttpPost]
public JsonResult Save([FromBody] User user)
{
try
{
2 years ago
var db = new ModelDbContext();
2 years ago
if (!ModelState.IsValid)
return Json(false);
2 years ago
db.Users.Add(user);
2 years ago
return Json(true);
}
catch (Exception ex)
{
Debug.WriteLine(ex);
return Json(false);
}
}
}
}