[HttpPost]
[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public ActionResult<VillaDTO> CreateVilla([FromBody] VillaCreateDTO villaDTO)
{
if (_db.Villas.FirstOrDefault(u => u.Name.ToLower() == villaDTO.Name.ToLower()) != null)
{
ModelState.AddModelError("CustomError", "Villa already Exists!");
return BadRequest(ModelState);
}
if (villaDTO == null)
{
return BadRequest(villaDTO);
}
Villa model = new()
{
Amenity = villaDTO.Amenity,
Details = villaDTO.Details,
ImageUrl = villaDTO.ImageUrl,
Name = villaDTO.Name,
Occupancy = villaDTO.Occupancy,
Rate = villaDTO.Rate,
Sqft = villaDTO.Sqft
};
_db.Villas.Add(model);
_db.SaveChanges();
// 会在response的head中增加一个location地址,指向新添加的地址
return CreatedAtRoute("GetVilla", new { id = model.Id }, model);
// return Ok(villaDTO);
}
标签:asp,return,Name,ProducesResponseType,StatusCodes,reponse,villaDTO,CreatedAtRoute
From: https://www.cnblogs.com/zhuoss/p/17674336.html