Buenas prácticas con Web API Net Core

5 views
Skip to first unread message

Carlos Admirador

unread,
May 14, 2021, 4:47:47 AMMay 14
to AltNet-Hispano
Qué buenas prácticas seguir al desarrollar Web API Net Core?

Usar ActionResult o no:

 [HttpGet]
public string Get()

 HttpGet]
public ActionResult<IEnumerable<string>> Get()


[HttpGet("{id}")]
public ActionResult<string> Get(int id)



Otras implementaciones más avanzadas:

[HttpGet]
 [Route("getByFilter")] 
 [ProducesResponseType(typeof(LoadResult), StatusCodes.Status200OK)] [ProducesResponseType((int)HttpStatusCode.BadRequest)]
public IActionResult GetByFilter(DataSourceLoadOptions loadOptions, string accountId, string activeState)
{
return Ok( _locationService.GetByFilter(loadOptions, accountId, activeState));
}

[HttpPost] 
 [Route("isHideAddButton")]
 [ProducesResponseType(typeof(APIOutput<bool>), StatusCodes.Status200OK)] [ProducesResponseType((int)HttpStatusCode.BadRequest)]
public APIOutput<bool> IsHideAddButton(IsHideAddButtonDTO isHideAddButtonDTO)
{
return SuccessOutput(_locationService.IsHideAddButton(isHideAddButtonDTO));

 [HttpPost] 
 [Route("getLocationListInfo")] [ProducesResponseType(typeof(APIOutput<LocationListInfoDTO>), StatusCodes.Status200OK)]
 [ProducesResponseType((int)HttpStatusCode.BadRequest)]
public APIOutput<LocationListInfoDTO> GetLocationListInfo(LocationListInfoFilterDTO filterDTO)
{
return SuccessOutput(_locationService.GetLocationListInfo(filterDTO));
}


Con Tasks y custom ActionResult:


[HttpGet("{userName}")]
public async Task<ResultDto<PersonalProfileDto>> Get(string userName)
{
return (await GetProfile(userName)).MapDto(x=>x);
}


[HttpPost]
public async Task<Result<PersonalProfileDto>> Post(PersonalProfileDto profileDto)
{
return await _postPersonalProfile.Create(profileDto)
.Bind(x => GetProfile(x.UserName));
}


[HttpPut]
public async Task<Result<PersonalProfileDto>> Put(PersonalProfileDto profileDto)
{
return await _putPersonalProfile.Create(profileDto)
.Bind(x => GetProfile(x.UserName));
}

Reply all
Reply to author
Forward
0 new messages