2014-03-26 1 views
0

나는이 작업을 얻으려고하는데 실패했습니다.PostAsJsonAsync 및 익명 형식 - 404 찾을 수 없음 오류

나는 API에 AttributeRouting을 사용하고 있는데 나는이 방법은 내 WebAPI에 정의 : 나는 다음과 같은 코드를 사용하여이 전화를 할 때

[POST("update"), JsonExceptionFilter] 
    public HttpResponseMessage PostUpdate([FromJson] long id, DateTime oriDt, string notes, int score) 

가 :

 using (var httpClient = new HttpClient(CreateAuthorizingHandler(AuthorizationState))) 
     { 
      var args = new { id, oriDt, notes, score }; 

      var postData = new List<KeyValuePair<string, string>>(); 
      postData.Add(new KeyValuePair<string, string>("id", id.ToString())); 
      postData.Add(new KeyValuePair<string, string>("oriDt", oriDt.ToString(_dateService.DefaultDateFormatStringWithTime))); 
      postData.Add(new KeyValuePair<string, string>("notes ", notes)); 
      postData.Add(new KeyValuePair<string, string>("score ", score.ToString(CultureInfo.InvariantCulture))); 

      var response = httpClient.PostAsJsonAsync(ApiRootUrl + "update", postData).Result; 

      if (response.IsSuccessStatusCode) 
      { 
       var data = response.Content.ReadAsAsync<bool>().Result; 
       return data; 
      } 

      return null; 
     } 

응답은 항상 404 - not found. 내가 여기서 무엇을 놓치고 있니? 동일한 문제가있는 코드에서 args이라는 익명 개체를 사용해 보았습니다.

나는 또한 동일한 결과를 가지고 [FromJson] 속성을 사용해 보았습니다.

답변

0

먼저 [FromJson]을 제거하십시오. 이것으로 당신은이 행동 방법을가집니다. 아래의 URI에 POST 경우

public HttpResponseMessage PostUpdate(long id, DateTime oriDt, string notes, int score)

, 그것은 작동합니다.

/업데이트/123? oridt = somedate & 노트 = somenote & 점수 = 89

당신이 (당신이 클라이언트 코드로 수행하는 것처럼) 필드를 게시 요청 본문을 사용하려는 경우 포함하는 클래스를 선언 요청 본문의 필드와 이름이 같은 속성

public class MyDto 
{ 
    public long Id { get; set; } 
    public DateTime OriDt { get; set; } 
    public string Notes { get; set; } 
    public int Score { get; set; } 
} 

그런 다음 동작 방법을 변경하십시오.

public HttpResponseMessage PostUpdate(MyDto dto)

관련 문제