.NET Core Web API 컨νΈλ‘€λ¬μμ HTTP μν μ½λλ‘ JSONμ λ°ννλ μ¬λ°λ₯Έ λ°©λ²μ μ°Ύκ³ μμ΅λλ€. λλ μ΄κ²μ λ€μκ³Ό κ°μ΄ μ¬μ©ν©λλ€ :
public IHttpActionResult GetResourceData()
{
return this.Content(HttpStatusCode.OK, new { response = "Hello"});
}
μ΄κ²μ 4.6 MVC μμ© νλ‘κ·Έλ¨μ μμμ§λ§ μ΄μ λ .NET Coreλ₯Ό μ¬μ©νμ¬ IHttpActionResultκ°μ§κ³ ActionResultμκ³ λ€μκ³Ό κ°μ΄ μ¬μ© νμ§ μλ κ² κ°μ΅λλ€ .
public ActionResult IsAuthenticated()
{
return Ok(Json("123"));
}
κ·Έλ¬λ μλ μ΄λ―Έμ§μ κ°μ΄ μλ²μ μλ΅μ μ΄μν©λλ€.
Web API 컨νΈλ‘€λ¬κ° Web API 2μμνλ κ²μ²λΌ HTTP μν μ½λλ‘ JSONμ λ°ννκΈ°λ₯Ό μν©λλ€.
λ΅λ³
a JsonResultλ‘ μλ΅νλ κ°μ₯ κΈ°λ³Έμ μΈ λ²μ μ λ€μ κ³Ό κ°μ΅λλ€.
// GET: api/authors
[HttpGet]
public JsonResult Get()
{
return Json(_authorRepository.List());
}
κ·Έλ¬λ μμ μ μλ΅ μ½λλ₯Ό λͺ μ μ μΌλ‘ μ²λ¦¬ ν μ ββμκΈ° λλ¬Έμ λ¬Έμ λ₯Ό ν΄κ²°νλ λ° λμμ΄λμ§ μμ΅λλ€.
μν κ²°κ³Όλ₯Ό μ μ΄νλ ββλ°©λ²
ActionResultμStatusCodeResultμ ν μ νμ©ν μμλ κ³³ μ λ°νν΄μΌνλ€λ κ²μ λλ€ .
μλ₯Ό λ€λ©΄ λ€μκ³Ό κ°μ΅λλ€.
// GET: api/authors/search?namelike=foo
[HttpGet("Search")]
public IActionResult Search(string namelike)
{
var result = _authorRepository.GetByNameSubstring(namelike);
if (!result.Any())
{
return NotFound(namelike);
}
return Ok(result);
}
μμ λ μμ λ λͺ¨λ Microsoft μ€λͺ μμμ μ 곡νλ νλ₯ν κ°μ΄λμμ μ 곡 ν κ²μ λλ€. μλ΅ λ°μ΄ν° νμν
μΆκ° 물건
λ΄κ° μμ£Ό μ νλ λ¬Έμ λ VSμ βNew Projectβν νλ¦Ώμμ κΈ°λ³Έ ꡬμ±μ μ¬μ©ν기보λ€λ WebAPIλ₯Όλ³΄λ€ μΈλ°νκ² μ μ΄νκΈ°λ₯Ό μνλ€λ κ²μ λλ€.
λͺ κ°μ§ κΈ°λ³Έ μ¬νμ΄ μλμ§ νμΈνμμμ€ β¦
1 λ¨κ³ : μλΉμ€ ꡬμ±
ASP.NET Core WebAPIκ° μν μ½λλ₯Ό μμ ν μ μ΄νλ©΄μ JSON Serialized Objectλ‘ μλ΅νλλ‘νλ €λ©΄ λ¨Όμ μμ μ°Ύμ λ©μλμ AddMvc()μλΉμ€λ₯Ό ν¬ν¨μμΌμΌν©λλ€ .ConfigureServicesStartup.cs
AddMvc()λ€λ₯Έ μμ² μ νμ λν μλ΅κ³Ό ν¨κ» JSON μ© μ λ ₯ / μΆλ ₯ ν¬λ§·ν°κ° μλμΌλ‘ ν¬ν¨ λλ€λ μ μ μ μν΄μΌ ν©λλ€.
νλ‘μ νΈμ λͺ¨λ κΆνμ΄ νμ application/jsonνκ³ λ€λ₯Έ μμ² μ ν (μ : νμ€ λΈλΌμ°μ μμ²)μ ν¬ν¨νμ¬ μλ΅νμ§ μλ λ€μν μμ² μ νμ λν WebAPIμ λμκ³Ό κ°μ μλΉμ€λ₯Ό μ격νκ² μ μνλ €λ κ²½μ° λ€μ μ½λ :
public void ConfigureServices(IServiceCollection services)
{
// Build a customized MVC implementation, without using the default AddMvc(), instead use AddMvcCore().
// https://github.com/aspnet/Mvc/blob/dev/src/Microsoft.AspNetCore.Mvc/MvcServiceCollectionExtensions.cs
services
.AddMvcCore(options =>
{
options.RequireHttpsPermanent = true; // does not affect api requests
options.RespectBrowserAcceptHeader = true; // false by default
//options.OutputFormatters.RemoveType<HttpNoContentOutputFormatter>();
//remove these two below, but added so you know where to place them...
options.OutputFormatters.Add(new YourCustomOutputFormatter());
options.InputFormatters.Add(new YourCustomInputFormatter());
})
//.AddApiExplorer()
//.AddAuthorization()
.AddFormatterMappings()
//.AddCacheTagHelper()
//.AddDataAnnotations()
//.AddCors()
.AddJsonFormatters(); // JSON, or you can build your own custom one (above)
}
λ€λ₯Έ μ§λ ¬ν νμ (protobuf, thrift λ±)μ μλ΅νλ €λ κ²½μ° μ¬μ©μ μ μ μ λ ₯ / μΆλ ₯ ν¬λ§·ν°λ₯Ό μΆκ° ν μμλ λ°©λ²λ ν¬ν¨λμ΄ μμ΅λλ€.
μμ μ½λ λ©μ΄λ¦¬λ λλΆλΆ AddMvc()λ©μλ μ 볡μ λ³Έμ
λλ€ . κ·Έλ¬λ ν
νλ¦Ώμ΄ ν¬ν¨ λ μ¬μ λ°°μ‘ λ μλΉμ€λ₯Ό μ¬μ©νλ λμ κ°κ°μ λͺ¨λ μλΉμ€λ₯Ό μ μνμ¬ κ° βκΈ°λ³ΈβμλΉμ€λ₯Ό μ체μ μΌλ‘ ꡬννκ³ μμ΅λλ€. μ½λ λΈλ‘μ 리ν¬μ§ν 리 λ§ν¬λ₯Ό μΆκ°νκ±°λ GitHub 리ν¬μ§ν 리μμ νμΈν μ μμ΅λλ€ AddMvc() ..
μ²μμλ κΈ°λ³Έκ°μ ꡬννμ§ μκ³ κΈ°λ³Έκ°μ βμ·¨μβνμ¬μ΄ λ¬Έμ λ₯Ό ν΄κ²°νλ €λ λͺ κ°μ§ μλ΄μκ° μμ΅λλ€. νμ¬ μ€ν μμ€λ‘ μμ νκ³ μλ€κ³ μκ°νλ©΄ μ€λ³΅ μμ μ λλ€. , λμ μ½λμ μμ§ν μ€λ μ¬λΌμ§ μ΅κ΄.
2 λ¨κ³ : 컨νΈλ‘€λ¬ μμ±
λλ λΉμ μκ² λΉμ μ μ§λ¬Έμ λΆλ₯νκΈ° μν΄ λΉμ μκ² μ λ§ κ°λ¨ν κ²μ 보μ¬μ€ κ²μ λλ€.
public class FooController
{
[HttpPost]
public async Task<IActionResult> Create([FromBody] Object item)
{
if (item == null) return BadRequest();
var newItem = new Object(); // create the object to return
if (newItem != null) return Ok(newItem);
else return NotFound();
}
}
3 λ¨κ³ : νμΈ λΉμ Content-Typeκ³ΌAccept
μμ²μ ν€λ Content-Typeμ Acceptν€λκ° μ¬λ°λ₯΄κ² μ€μ λμ΄ μλμ§ νμΈν΄μΌν©λλ€ . κ·νμ κ²½μ° (JSON), λΉμ μ κ·Έκ²μ μ€μ νκ³ μΆμ κ²μ
λλ€application/json .
μμ² ν€λμ μ§μ μ κ΄κ³μμ΄ WebAPIκ° κΈ°λ³Έκ°μΌλ‘ JSONμΌλ‘ μλ΅νλλ‘νλ €λ©΄ λͺ κ°μ§ λ°©λ²μΌλ‘ μν ν μ μμ΅λλ€ .
λ°©λ² 1
μ΄μ μ κΆμ₯ ν κΈ°μ¬ ( μλ΅ λ°μ΄ν° νμν )μ νμλ κ²μ²λΌ 컨νΈλ‘€λ¬ / μ‘μ
μμ€μμ νΉμ νμμ κ°μ ν μ μμ΅λλ€. λλ κ°μΈμ μΌλ‘μ΄ μ κ·Όλ²μ μ’μνμ§ μμ§λ§ β¦ μμ μ±μμν κ²μ
λλ€.
νΉμ νμ κ°μ μ μ© κ°λ₯ν νΉμ μμ μ λν μλ΅ νμμ μ ννλ €λ©΄ [μ μ] νν°λ₯Ό μ μ© ν μ μμ΅λλ€. [Produces] νν°λ νΉμ μμ (λλ 컨νΈλ‘€λ¬)μ λν μλ΅ νμμ μ§μ ν©λλ€. λλΆλΆμ νν°μ λ§μ°¬κ°μ§λ‘μ΄ κ°μ μμ , 컨νΈλ‘€λ¬ λλ μ μ λ²μμμ μ μ© ν μ μμ΅λλ€.
[Produces("application/json")] public class AuthorsController
[Produces]νν°λ λ΄μ λͺ¨λ μμ μ κ°μ λ‘
AuthorsControllerλ€λ₯Έ ν¬λ§€ν°κ° μμ© νλ‘κ·Έλ¨μ λν κ΅¬μ± λ° ν΄λΌμ΄μΈνΈκ° μ 곡 ν κ²½μ°μλ, JSON νμμ μλ΅μ λ°ννλAcceptλ€λ₯Έ κ°λ₯ν νμμ μμ² ν€λλ₯Ό.
λ°©λ² 2
μ νΈνλ λ°©λ²μ WebAPIκ° μμ² λ νμμΌλ‘ λͺ¨λ μμ²μ ββμλ΅νλ κ²μ
λλ€. κ·Έλ¬λ,μ΄ μꡬ λ νμμ νμ©νμ§ μλ κ²½μ°μ, λ€μ κ°μ β λ€μ μ΄κΈ° μνλ‘ (μ¦. JSON)
λ¨Όμ μ΅μ μ λ±λ‘ν΄μΌν©λλ€ (μμ μΈκΈ νλλ‘ κΈ°λ³Έ λμμ λ€μ μμ ν΄μΌ ν¨).
options.RespectBrowserAcceptHeader = true; // false by default
λ§μ§λ§μΌλ‘, μλΉμ€ λΉλμ μ μ λ ν¬λ§·ν° λͺ©λ‘μ μμλ₯Ό λ€μ μ λ ¬νλ©΄ μΉ νΈμ€νΈλ λͺ©λ‘μ 맨 μμ μμΉνλ ν¬λ§·ν° (μ : μμΉ 0)λ‘ κΈ°λ³Έ μ€μ λ©λλ€.
μμΈν λ΄μ©μμ΄ .NET μΉ κ°λ° λ° λꡬ λΈλ‘κ·Έ νλͺ©μ μ°Έμ‘°νμμμ€.
λ΅λ³
κ°μ₯ μΌλ°μ μΈ μν μ½λμ λν΄ λ―Έλ¦¬ μ μ λ λ°©λ²μ΄ μμ΅λλ€.
Ok(result)200μλ΅κ³Ό ν¨κ» λ°νCreatedAtRoute201+ μλ‘μ΄ λ¦¬μμ€ URLμ λ°νNotFound보κ³404BadRequest400λ±μ λ°ν
μ°Έμ‘° BaseController.csλ° Controller.csλͺ¨λ λ©μλμ λͺ©λ‘.
κ·Έλ¬λ μ λ§λ‘ μ£Όμ₯νλ€λ©΄ StatusCodeμ¬μ©μ μ μ μ½λλ₯Ό μ€μ νλ λ° μ¬μ©ν μλ μμ§λ§ μ½λλ₯Ό μ½μ μ μκ² λ§λ€κ³ ν€λλ₯Ό μ€μ νκΈ° μν΄ μ½λλ₯Ό λ°λ³΅ν΄μΌν©λλ€ (μ κ°μ CreatedAtRoute).
public ActionResult IsAuthenticated()
{
return StatusCode(200, "123");
}
λ΅λ³
ASP.NET Core 2.0μ μ¬μ©νλ©΄ κ°μ²΄λ₯Ό λ°ννλ μ΄μμ μΈ λ°©λ² Web APIμ MVCμ ν΅ν©λκ³ λμΌν κΈ°λ³Έ ν΄λμ€λ₯Ό μ¬μ©νλ κ² Controllerμ
λλ€.
public IActionResult Get()
{
return new OkObjectResult(new Item { Id = 123, Name = "Hero" });
}
κ·Έκ²μμ£Όμν΄λΌ
200 OKμν μ½λ μ ν¨κ» λ°νλ©λλ€ (μOkμ ν μObjectResult)- μ½ν
μΈ νμμ μνν©λλ€. μ¦,
Acceptμμ² ν€λμ λ°λΌ λ°ν λ©λλ€.Accept: application/xmlμμ²μ΄ μ μ‘ λλ©΄ λ‘ λ°νλ©λλ€XML. μ무κ²λ 보λ΄μ§ μμΌλ©΄JSONκΈ°λ³Έκ°μ λλ€.
νΉμ μν μ½λμ ν¨κ» 보λ΄μΌ νλ ObjectResultκ²½μ° StatusCodeλλλ₯Ό μ¬μ©νμμμ€. λ λ€ λμΌν μμ
μ μννκ³ μ»¨ν
μΈ νμμ μ§μν©λλ€.
return new ObjectResult(new Item { Id = 123, Name = "Hero" }) { StatusCode = 200 };
return StatusCode( 200, new Item { Id = 123, Name = "Hero" });
λλ ObjectResultλ‘ λ μΈλΆννμμμ€.
Microsoft.AspNetCore.Mvc.Formatters.MediaTypeCollection myContentTypes = new Microsoft.AspNetCore.Mvc.Formatters.MediaTypeCollection { System.Net.Mime.MediaTypeNames.Application.Json };
String hardCodedJson = "{\"Id\":\"123\",\"DateOfRegistration\":\"2012-10-21T00:00:00+05:30\",\"Status\":0}";
return new ObjectResult(hardCodedJson) { StatusCode = 200, ContentTypes = myContentTypes };
νΉλ³ν JSON μΌλ‘ λ°ν νλ €λ©΄ λͺ κ°μ§ λ°©λ²μ΄ μμ΅λλ€.
//GET http://example.com/api/test/asjson
[HttpGet("AsJson")]
public JsonResult GetAsJson()
{
return Json(new Item { Id = 123, Name = "Hero" });
}
//GET http://example.com/api/test/withproduces
[HttpGet("WithProduces")]
[Produces("application/json")]
public Item GetWithProduces()
{
return new Item { Id = 123, Name = "Hero" };
}
κ·Έκ²μμ£Όμν΄λΌ
- λ λ€
JSONμλ‘ λ€λ₯Έ λ κ°μ§ λ°©μμΌλ‘ μν λ©λλ€. - λ λ€ μ½ν μΈ νμμ 무μν©λλ€.
- 첫 λ²μ§Έ λ°©λ²μ νΉμ serializerλ‘ JSONμ μ μ©
Json(object)ν©λλ€. - λ λ²μ§Έ λ°©λ²μ
Produces()μμ± (ResultFilter)μcontentType = application/json
곡μ λ¬Έμ μμ μμΈν λ΄μ©μ μ½μ΄λ³΄μμμ€ . μ¬κΈ°μμ νν°μ λν΄ μμ보μμμ€ .
μνμ μ¬μ©λλ κ°λ¨ν λͺ¨λΈ ν΄λμ€
public class Item
{
public int Id { get; set; }
public string Name { get; set; }
}
λ΅λ³
λ΄κ° μκ°ν΄ λΈ κ°μ₯ μ¬μ΄ λ°©λ²μ λ€μκ³Ό κ°μ΅λλ€.
var result = new Item { Id = 123, Name = "Hero" };
return new JsonResult(result)
{
StatusCode = StatusCodes.Status201Created // Status code here
};
λ΅λ³
μ΄κ²μ΄ κ°μ₯ μ¬μ΄ ν΄κ²°μ± μ λλ€.
public IActionResult InfoTag()
{
return Ok(new {name = "Fabio", age = 42, gender = "M"});
}
λλ
public IActionResult InfoTag()
{
return Json(new {name = "Fabio", age = 42, gender = "M"});
}
λ΅λ³
enumμ μ¬μ©νμ¬ 404/201 μν μ½λλ₯Ό μ¬μ©νλ λμ
public async Task<IActionResult> Login(string email, string password)
{
if (string.IsNullOrWhiteSpace(email) || string.IsNullOrWhiteSpace(password))
{
return StatusCode((int)HttpStatusCode.BadRequest, Json("email or password is null"));
}
var user = await _userManager.FindByEmailAsync(email);
if (user == null)
{
return StatusCode((int)HttpStatusCode.BadRequest, Json("Invalid Login and/or password"));
}
var passwordSignInResult = await _signInManager.PasswordSignInAsync(user, password, isPersistent: true, lockoutOnFailure: false);
if (!passwordSignInResult.Succeeded)
{
return StatusCode((int)HttpStatusCode.BadRequest, Json("Invalid Login and/or password"));
}
return StatusCode((int)HttpStatusCode.OK, Json("Sucess !!!"));
}
λ΅λ³
λ΄κ° μ¬κΈ°μ μ°Ύμ λ©μ§ λλ΅κ³Ό λλμ΄ λ°ν μ§μ μ 보μκ³ StatusCode(whatever code you wish)ν¨κ³Όκ°μμμ΅λλ€ !!!
return Ok(new {
Token = new JwtSecurityTokenHandler().WriteToken(token),
Expiration = token.ValidTo,
username = user.FullName,
StatusCode = StatusCode(200)
});