public IActionResult Create(string goodsName, string goodsDescription) { GoodsBase model = new GoodsBase() { GoodsName = goodsName, GoodsDescription = goodsDescription }; _repository.AddGoods(model); //return Redirect("/Goods"); // 리스트 페이지로 이동 return(RedirectToAction(nameof(Index))); }
[Consumes("application/json")] // application/xml public IActionResult Post([FromBody] GoodsBase model) // Deserialize, 생성 전용 DTO 클래스 사용 가능 { // 예외 처리 방법 if (model == null) { return(BadRequest()); // Status: 400 Bad Request } try { // 예외 처리 if (model.GoodsName == null || model.GoodsName.Length < 1) { ModelState.AddModelError("GoodsName", "제품명을 입력해야 합니다."); } // 모델 유효성 검사 if (!ModelState.IsValid) { return(BadRequest(ModelState)); // 400 에러 출력 } var m = _repository.AddGoods(model); // 저장 //return Ok(m); // 200 OK //return CreatedAtRoute("GetGoodsById", new { id = m.GoodsId }, m); // Status: 201 Created if (DateTime.Now.Second % 2 == 0) //[!] 2가지 방식 중 원하는 방식 사용 { //return CreatedAtAction("GetGoodsById", new { id = m.GoodsId }, m); return(CreatedAtRoute("GetGoodsById", new { id = m.GoodsId }, m)); // Status: 201 Created } else { var uri = Url.Link("GetGoodsById", new { id = m.GoodsId }); return(Created(uri, m)); // 201 Created } } catch (Exception ex) { return(BadRequest($"에러가 발생했습니다. {ex.Message}")); } }