ActionResult<T>

Core 2.1부터 생긴 ActionResult 관련 내용입니다.

일단 서로 다른 형식의 return 형과 object를 ActionResult를 통해 전달받을 수 있다!! 는 편리함이 있습니다.

ex01
1
2
3
4
5
6
7
8
9
10
public ActionResult<모델>
{
...진입 예외처리...

var 모델 = Get모델(키);

...처리처리....

return 모델
}
MicrosoftDOC 링크

위의 링크에 나온데로 ActionResult에서 파생된 형식을 반환하거나 특정형식(Json, XML, etc)을 반환할 수 있습니다.!!!!!

ex01
1
2
3
4
5
6
7
8
9
10
11
12
13
// GET: api/Todo/5
[HttpGet("{id}")]
public async Task<ActionResult<TodoItem>> GetTodoItem(long id)
{
var todoItem = await _context.TodoItems.FindAsync(id);

if (todoItem == null)
{
return NotFound();
}

return todoItem;
}

이런식의 사용이 가능합니다, IActionResult(IActionResult Interface) 를 사용했을때 라면 CAST과정이 필요했었다고 저는 기억하고 있습니다, 그런데 ActionResult는 T 의 ObjectResult로 변환합니다(암시적 캐스트 연산자 T)

쓸때없는 말들로 앞을 채웠지만…편하다!!! 써보니 편리하고 선언시점에서 어노테이션을 주어서 명시적으로 캐스트해서 ActionResult를 받던 방법에서는 Swagger같은데서 잘 못읽는 경우…뭐…..대부분 잘 읽었어요 사실…다들 아시잖아요…그런데 엄청 길게 갔을때의 좀더 명확한 코딩이 가능하다 맘에든다 는 의미의 포스팅이었습니다..

ㅋㅋ 감사합니다.

Author: jklee@lenscloth.io
Link: https://lenscloth-ko.github.io.git/2019/02/01/ActionResult/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.