C# 在ASP.net MVC中通过web服务向数据库发送数据
在一个colution文件下,我有两个projects.UI(ASP.NETMVC)项目、一个rest服务项目、一个DTO项目、一个业务逻辑项目和数据访问。现在,我的服务项目的控制器需要与我的UI(ASP.Net MVC)项目的控制器进行对话,获取表单中输入的数据并将其发送到数据库。我很不确定我应该在UI项目的控制器类中提出什么逻辑。UI项目sepratley也有实体类。需要帮助C# 在ASP.net MVC中通过web服务向数据库发送数据,c#,asp.net-mvc,web-services,rest,C#,Asp.net Mvc,Web Services,Rest,在一个colution文件下,我有两个projects.UI(ASP.NETMVC)项目、一个rest服务项目、一个DTO项目、一个业务逻辑项目和数据访问。现在,我的服务项目的控制器需要与我的UI(ASP.Net MVC)项目的控制器进行对话,获取表单中输入的数据并将其发送到数据库。我很不确定我应该在UI项目的控制器类中提出什么逻辑。UI项目sepratley也有实体类。需要帮助 This is the POST method in my services controller // POST
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的方法。此方法位于我的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);
}
[HttpPost,路由(“/maintenance/CreateMaintenanceOrder”)]
public PartialViewResult CreateMaintenanceOrder([FromBody]维护模型)
{
Model.CheckItems=维护视图Model.CheckItems;
Model.CrewMembers=维护视图Model.CrewMembers;
Model.WorkID=++SessionUtility.CurrentMaintenanceID;
尝试
{
var uri=“api/Maintenance/Post”;
使用(HttpClient HttpClient=new HttpClient())
{
httpClient.BaseAddress=新Uri(“http://localhost:8961");
任务请求=httpClient.GetStringAsync(uri);
Model=JsonConvert.DeserializeObject(request.Result);
}
maintenanceViewModel.MaintenanceOrders.Add(模型);
}
捕获(聚合异常e)
{
}
maintenanceViewModel.MaintenanceOrders.Add(模型);
返回PartialView(“u搜索”,maintenanceViewModel);
}
您试图使用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));
您正在尝试使用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));
@R Jain我在使用HttpResponseMessage时遇到一个错误,它说“不能隐式地将system.threading.tasks.task类型转换为system.net.http.http响应消息”表示歉意,您可以不使用HttpResponseMessage而直接将其分配给任务请求,就像原始消息一样message@RJain我在使用HttpResponseMessage时出错“无法将system.threading.tasks.task类型隐式转换为system.net.http.http响应消息”表示歉意,您可以只将其分配给任务请求,而不使用HttpResponseMessage,如原始消息中所示