示例#1
0
      public ActionResult <CommandReadDto> CreateCommand(CommandCreateDto commandcreatDto)
      {
          var commadmodel = _mapper.Map <Command>(commandcreatDto);

          _repository.createCommand(commadmodel);
          _repository.SaveCahanges(); // without this line the data will not be saved to Db.
          var commandReadDto = _mapper.Map <CommandReadDto>(commadmodel);

//return Ok(commandReadDto); : This line works but give us a a 200 resonse ok. Actually as per architectual bes practices
//need a created reulst and a created location in the response header. For that purpose we can use the below syntax.


          return(CreatedAtRoute(nameof(GetCommandById), new { id = commandReadDto.Id }, commandReadDto));


          //     Creates a Microsoft.AspNetCore.Mvc.CreatedAtRouteResult object that produces
          //     a Microsoft.AspNetCore.Http.StatusCodes.Status201Created response.
      }