2014-01-18 67 views
2

我想從我的winforms應用程序發佈一個對象列表到我的asp.net mvc 4網站。我測試過發佈一個對象,它可以工作,但不適用於列表。它返回一個500 (Internal Server Error)。這裏是我的代碼:ASP.NET MVC Web API:發佈對象列表

ASP.NET MVC的Web 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; 
     } 
    } 
} 

編輯:

我發現了錯誤的場景,在我的api控制器中有兩種方法,甚至認爲它們有不同的簽名。如果我評論一種方法,郵件工作正常(項目或列表)。有任何想法嗎 ?

+0

您是否看到500響應中的錯誤消息。如果是這樣,你能分享它是什麼嗎? –

+0

@KiranChalla:不幸的是,沒有消息,即使在跟蹤頁面(localhost/mywebsite/trace.axd) – SidAhmed

回答

2

這些方法可能具有不同的簽名,但Web API無法在不檢查正文的情況下分辨它們之間的區別,但由於性能原因,它不會執行這些操作。

您可以做兩件事 - 創建一個只包含WebTrace對象列表的新類,並將其放入不同的API控制器,或者將自定義路線映射到您現有的一種方法。你可以用ActionName屬性來做到這一點,不過,我可能會採取第一種方法。

+1

+1爲了解釋,你的迴應更有幫助。儘管第一種方法看起來很誘人,但我選擇使用路由方法來維護原因,並且更具體地說,屬性路由(http://www.asp.net/web-api/overview/web-api -routing - 和 - 的動作/屬性的路由功能於web的API-2)。再次感謝:-) – SidAhmed

+0

是屬性路由是現在走的路,不是ActionName屬性。謝謝我會更新答案 –