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);
}
@R耆那教我使用HttpResponseMessage说:“不能隐式转换类型system.threading.tasks.task到system.net时得到一个错误。 http.http响应消息“ –
道歉,而不是使用HttpResponseMessage,您可以将它分配给任务请求,如您在原始消息 –