2016-10-04 103 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,您可以将它分配给任务请求,如您在原始消息 –

相关问题