Created
July 31, 2022 04:33
-
-
Save mortezadalil/51025816983b3629e65d69bc2a147b6b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Text.Json; | |
using controllers.ApiModels; | |
using Microsoft.AspNetCore.Mvc; | |
namespace controllers.Controllers; | |
[ApiController] | |
[Route("Demo")] | |
public class DemoController : ControllerBase | |
{ | |
// different methods that return an author | |
[HttpGet("getobject")] | |
public object GetObject() | |
{ | |
return GetBook(); | |
} | |
// returns a 204 no content with no body | |
// response type header is not set | |
[HttpGet("getobjectnull")] | |
public object GetObjectNull() | |
{ | |
return null; | |
} | |
// will set response type to text/plain | |
[HttpGet("getstring")] | |
public string GetString() | |
{ | |
return JsonSerializer.Serialize(GetBook()); | |
} | |
[HttpGet("getjson")] | |
public JsonResult GetJson() | |
{ | |
return new JsonResult(GetBook()); | |
} | |
[HttpGet("getiactionresult")] | |
public IActionResult GetIActionResult() | |
{ | |
return Ok(GetBook()); | |
} | |
[HttpGet("getiactionresult<T>")] | |
public ActionResult<BookDto> GetActionResultOfT() | |
{ | |
return Ok(GetBook()); | |
} | |
private BookDto GetBook() | |
{ | |
return new BookDto(1, "1234-5678", "Warren Piece", "Steve Smith"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment