2016-10-04 134 views
1

在一個colution文件下,我有幾個projects.UI(ASP.Net mvc)項目,我的休息服務項目,DTO項目,業務邏輯項目和數據訪問。現在我的服務項目的控制器需要與我的UI(ASP.Net MVC)項目的控制器交談,並獲取輸入到表單中的數據並將其發送到數據庫。我很不確定我應該在UI項目的控制器類中想出的邏輯。 UI項目sepratley也有實體類。需要幫助!!通過Web服務發送數據到數據庫在ASP.net MVC

This is the POST method in my services controller 

// POST api/Maintenance 
    [HttpPost] 
    public IHttpActionResult Post([FromBody]Maintenance maintenance) 
    { 
     if (!ModelState.IsValid) 
     { 
      return BadRequest(ModelState); 
     } 
     maintenanceLogic.Insert(maintenance); 

     return CreatedAtRoute("DefaultApi", new { id = maintenance.WorkID }, maintenance); 
    } 

這是將訪問上述方法的方法uri.This方法在我的UI項目的控制器類中。我想出了一個邏輯,但我認爲它不正確。

 [HttpPost, Route("/maintenance/CreateMaintenanceOrder")] 
    public PartialViewResult CreateMaintenanceOrder([FromBody] Maintenance Model) 
    { 
     Model.CheckItems = maintenanceViewModel.CheckItems; 
     Model.CrewMembers = maintenanceViewModel.CrewMembers; 
     Model.WorkID = ++SessionUtility.CurrentMaintenanceID; 
     try 
     { 
      var uri = "api/Maintenance/Post "; 

      using (HttpClient httpClient = new HttpClient()) 
      { 
       httpClient.BaseAddress = new Uri("http://localhost:8961"); 
       Task<String> request = httpClient.GetStringAsync(uri); 
       Model = JsonConvert.DeserializeObject<List<Maintenance>>(request.Result); 
      } 
      maintenanceViewModel.MaintenanceOrders.Add(Model); 

     } 
     catch (AggregateException e) 
     { 

     } 

     maintenanceViewModel.MaintenanceOrders.Add(Model); 
     return PartialView("_Search", maintenanceViewModel); 
    } 

回答

0

您正在試圖調用使用GetAsync POST方法以及看來你是不及格被作爲輸入REST服務方法所需的維護對象。請嘗試以下 -

string maintenanceData = JsonConvert.SerializeObject(Model); //considering you have to pass Model as input to Rest method 
HttpResponseMessage response = httpClient.PostAsync(new Uri("http://localhost:8961" + "api/Maintenance/Post/"), new StringContent(maintenanceData)); 
+0

@R耆那教我使用HttpResponseMessage說:「不能隱式轉換類型system.threading.tasks.task 到system.net時得到一個錯誤。 http.http響應消息「 –

+0

道歉,而不是使用HttpResponseMessage,您可以將它分配給任務請求,如您在原始消息 –

相關問題