수색…


비고

ASP.NET WebAPI를 사용하여 신속하게 (올바르게) 실행하는 예제

특성을 사용하여 GET에서 JSON 반환

1. Register of ( App_Start/WebApiConfig )에서 포맷터 및 라우팅을 설정하십시오.

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        GlobalConfiguration.Configuration.Formatters.Clear();
        GlobalConfiguration.Configuration.Formatters.Add(new JsonMediaTypeFormatter());

        config.MapHttpAttributeRoutes();
    }
}

2. ApiController 에서 메소드 만들기

public class HelloWorldController : ApiController
{
  [HttpGet]
  [Route("echo/{message}")]
  public IHttpActionResult Echo(string message) {
    return Ok(new{ hello: message });  
  }

  [HttpGet]
  [Route("echo/{digits:int}")]
  public IHttpActionResult Echo(int digits) {
    return Ok(new{ hello: digits });  

}

GET /echo/foo 실행하기

{
  "hello": "foo"
}

GET /echo/1241290805 실행

{
  "hello": 1241290805
}

방법을 선택할 때 라우팅 프레임 워크가 가장 구체적인 조건 (데이터 유형)을 취하기 때문에



Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow