2014-11-27 8 views
0

MVC web api 메서드를 다른 이름으로 실행하는 방법은 GET 및 POST입니다. 나는 실행할 수 없다. 그것은 오류를 보여줍니다. 여기 내 코드가있다.GET 및 POST를 사용하는 MVC 웹 API

webapiconfig.cs

  config.Routes.MapHttpRoute(
      name: "DefaultApi", 
      routeTemplate: "api/{controller}/{id}", 
      defaults: new { action = "get", id = RouteParameter.Optional } 
      ); 

직원 컨트롤러

는 :

[HttpGet] 
    public string Test1() 
    { 
     return "this is a test1 string"; 
    } 

    [HttpGet] 
    public string Test2() 
    { 
     return "this is a test2 string"; 
    } 
    [HttpPost] 
    public string Test3() 
    { 
     return "this is a test3 string"; 
    } 

난 두 게시물을 실행하고 다른 시나리오에서 모든 방법을 얻을합니다. 그것을하는 방법?

답변

0

  • 은 (HttpGet/HttPost/HttpPut)의 HTTP 메소드와 동일한 메소드에 대한 다른 입력 파라미터를 지정 달성하는 방법은 두 가지가있다.

예 -

[HttpGet] 
     public string Test1(string input) 
     { 
      return "this is a test1 string"; 
     } 

[HttpGet] 
    public string Test2() 
    { 
     return "this is a test2 string"; 
    } 
  • HttpGet가 여러 제어기 사용 방법의 각각에 대하여 명시 적으로 다른 경로를 지정한다.

    [HttpGet] [도로 ("~/MYCONTROLLER/액션")] 공개 Test1을 문자열 (입력 문자열) { 복귀 "이것은 TEST1 문자열 인"; }

    [HttpGet] 
        [Route("~/mycontroller/action2")] 
        public string Test2() 
        { 
         return "this is a test2 string"; 
        } 
    
+0

형식과 사투를 벌인. 서식있는 행동 양식. 누구든지 포맷을 수행 할 수 있습니다. –

관련 문제