mardi 24 septembre 2019

Net Core: Find Data Type from Controller Action Method

How do I find the Return Type T from a Controller Action Method? I want to fill this with Reflection Later

GetBookResponse, I need to look at Return Type from Controller Action. This can error out sometimes, with error "System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'"

Type returnType = action.ActionMethod.ReturnType.GenericTypeArguments[0].GetGenericArguments()[0];

from Code base:

[HttpGet("[Action]/{id}")]
[ProducesResponseType(typeof(GetBookResponse), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(typeof(GetBookResponse), StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<GetBookResponse>> GetByBook(int id)
{
   var book = await bookservice.GetBookById(id);
   return Ok(book);
}

Referring question,

Net Core API: Make ProducesResponseType Global Parameter or Automate





Aucun commentaire:

Enregistrer un commentaire