2014-01-18 7 views
2

내 winforms 응용 프로그램에서 my asp.net mvc 4 웹 사이트에 개체 목록을 게시하려고합니다. 하나의 객체를 게시 테스트 한 결과 작동하지만 목록에 대해 작동하지 않습니다. 500 (Internal Server Error)을 반환합니다. 여기 내 코드입니다 :ASP.NET MVC 웹 API : 개체 목록 게시

:

ASP.NET MVC 웹 API

public class PostTraceController : ApiController 
{ 
    public HttpResponseMessage Post(List<WebTrace> list) 
    { 
     try 
     { 
      // Some code 
      return Request.CreateResponse(HttpStatusCode.Created); 
     } 
     catch (Exception ex) 
     { 
      HttpContext.Current.Trace.Write("exception", ex.Message); 
      return Request.CreateErrorResponse(HttpStatusCode.ServiceUnavailable, ex); 
     } 
    } 

    public HttpResponseMessage Post(WebTrace item) 
    { 
     try 
     { 
      // Some code 
      return Request.CreateResponse(HttpStatusCode.Created); 
     } 
     catch (Exception ex) 
     { 
      HttpContext.Current.Trace.Write("exception", ex.Message); 
      return Request.CreateErrorResponse(HttpStatusCode.ServiceUnavailable, ex); 
     } 
    } 
} 

상금 응용 프로그램

public class BaseSender 
{ 
    public BaseSender() 
    { 
     Client = new HttpClient 
     { 
      BaseAddress = new Uri(@"http://localhost/mywebsite/") 
     }; 

     Client.DefaultRequestHeaders.Accept.Add(
      new MediaTypeWithQualityHeaderValue("application/json")); 
    } 

    public string UserCode { get; set; } 
    protected readonly HttpClient Client; 

    public HttpResponseMessage PostAsJsonAsync(string requestUri, object value) 
    { 
     var response = Client.PostAsJsonAsync(requestUri, value).Result; 
     response.EnsureSuccessStatusCode(); 
     return response; 
    } 
} 

public class WebTraceSender : BaseSender 
{ 
    private const string requestUri = "api/posttrace"; 

    public bool Post(List<ArchiveCptTrace> list) 
    { 
     try 
     { 
      var listWebTrace = new List<WebTrace>(); 
      foreach (var item in list) 
      { 
       listWebTrace.Add(new WebTrace 
        { 
         DateStart = item.DatePreparation, 
         DateEnd = item.DateCloture, 
         UserStart = item.UserPreparation.UserName, 
         UserEnd = item.UserCloture.UserName, 
         AmountStart = item.MontantPreparation, 
         AmountEnd = item.MontantCloture, 
         TheoricAmountEnd = item.MontantTheorique, 
         Difference = item.Ecart, 
         UserCode = UserCode 
        }); 
      } 

      var responce = PostAsJsonAsync(requestUri, listWebTrace); 
      return responce.IsSuccessStatusCode; 
     } 
     catch (Exception e) 
     { 
      // TODO : Trace the exception 
      return false; 
     } 
    } 
} 

EDIT 형성

나는 api 컨트롤러에 두 가지 메소드를 가지고있는 오류의 시나리오를 발견했습니다. 심지어 다른 서명이 있다고 생각합니다. 하나의 메소드에 주석을 달면 게시물이 잘 작동합니다 (항목 또는 목록). 어떤 아이디어?

+0

500 응답에 오류 메시지가 표시됩니까? 그렇다면 공유 할 수 있습니까? –

+0

@KiranChalla : 아쉽게도 추적 페이지 (localhost/mywebsite/trace.axd)에있는 메시지가 없습니다. – SidAhmed

답변

2

메서드마다 서명이 다를 수 있지만 성능면에서 보았을 때 본문을 검사하지 않고도 Web API에서 차이점을 알 수 없습니다.

두 가지 작업을 수행 할 수 있습니다. 즉, WebTrace 개체 목록을 보유하고있는 새 클래스를 만들어 다른 API 컨트롤러에 넣거나 기존 메서드 중 하나에 사용자 지정 경로를 매핑 할 수 있습니다. 당신은 ActionName 속성으로 할 수 있지만, 아마도 첫 번째 방법을 택할 것입니다.

+1

+1 설명과 도움이 도움이되었습니다. 첫 번째 방법은 매혹적 인 것으로 보이지만, 나는 유지 보수의 이유로 라우팅 접근 방식을 선택했습니다. 더 자세한 내용은 Attribute Routing (http://www.asp.net/web-api/overview/web-api)을 참조하십시오. -routing-and-actions/속성 라우팅 in-web-api-2). 다시 감사 :-) – SidAhmed

+0

예 속성 라우팅은 ActionName 속성이 아닌 지금가는 길입니다. 고마워, 나는 대답을 업데이트 할 것이다. –