2013-08-13 5 views
0

여기 내 컨트롤러입니다.웹 API : PUT/POST 메서드가 작동하지 않습니다.

public class ProductionStateController : ApiController 
    { 
     private readonly IFranchiseService _franchiseService; 
     public ProductionStateController(IFranchiseService franchiseService) 
     { 
      _franchiseService = franchiseService; 
     } 

     [DataContext] 
     public string PutProductionState(int id, FranchiseProductionStates state) 
     { 
      _franchiseService.ChangeProductionState(id, state); 

      var redirectToUrl = "List"; 

      return redirectToUrl; 
     } 
    } 

내 아약스 호출;

self.selectState = function (value) { 
       $.ajax({ 
        url: "/api/ProductionState", 
        type: 'PUT', 
        contentType: 'application/json', 
        data: "id=3&state=Pending", 
        success: function (data) { 
         alert('Load was performed.'); 
        } 
       }); 
      }; 

내 경로;

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

나는 404 File not found 오류가 발생합니다.

POST으로 변경하면 동일합니다.

내가 만들면 GET 모든 것이 작동합니다.

여기에 뭔가가 누락되었습니다. 어떤 도움이라도 대단히 감사하겠습니다.

답변

1

웹 API 프레임 워크는 http 동사로 시작하는 작업 방법과 일치합니다. 따라서 PutProductionState은 이름으로 확인됩니다.

이 작업을 수행 할 수있었습니다. 문제는 다음과 같다 : ID 파라미터

self.selectState = function (value) { 
       $.ajax({ 
        url: "/api/ProductionState/3", 
        type: 'PUT', 
        contentType: 'application/json', 
        data: "'Pending'", 
        success: function (data) { 
         alert('Load was performed.'); 
        } 
       }); 
      }; 

주 :이 같아야

public string PutProductionState(int id, [FromBody] FranchiseProductionStates state) 
     { 
      _franchiseService.ChangeProductionState(id, state); 

      var redirectToUrl = "List"; 

      return redirectToUrl; 
     } 

그리고 AJAX 호출 : 동작 방법의 두 번째 파라미터는 [FromBody] 특성으로 표시되어야 URL과 문자열로 된 데이터에 추가됩니다.

감사합니다.

0
<script> 
function CallData(ids) { 
    debugger; 
    if (ids != null) { 
     $.ajax({ 
      url: "EVENT To Call (Which is in Controller)", 
      data: { 
       SelId: $("#Control").val() 
      }, 
      dataType: "json", 
      type: "POST", 
      error: function() { 
       alert("Somehitng went wrong.."); 
      }, 
      success: function (data) { 
       if (data == "") { 
        //Do Your tuff 
       } 
      } 
     }); 
    } 
} 

// 컨트롤러

에서
[HttpPost] 
public ActionResult EVENT To Call (Which is in Controller) (int ids) 
{ 
    //Do Your Stuff 
return Json(Your Object, JsonRequestBehavior.AllowGet); 
} 
관련 문제