[英]Sending data through web service to databse in ASP.net MVC
Under one colution file I have couple of projects.UI (ASP.Net mvc) project, A project for my rest services, A DTO project, A business logic project and Data Access. 在一个colution文件下,我有几个项目.UI(ASP.Net mvc)项目,我的休息服务项目,A DTO项目,业务逻辑项目和数据访问。 Now my services projects's controller needs to talk with my UI(ASP.Net MVC) project's controller and get the data entered in the form and send it to the database.
现在我的服务项目的控制器需要与我的UI(ASP.Net MVC)项目的控制器交谈,并获取在表单中输入的数据并将其发送到数据库。 Im quite unsure of the logic I should come up with inside the controller class of the UI project.
我完全不确定我应该在UI项目的控制器类中提出的逻辑。 The UI project sepratley has entity classes as well.
UI项目sepratley也有实体类。 Help needed!!
需要帮助!!
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);
}
This is the method that will access the above methods uri.This method is in the controller class of my UI project. 这是将访问上述方法uri的方法。此方法位于我的UI项目的控制器类中。 I came up with a logic.But I think its not correct.
我提出了一个逻辑。但我认为它不正确。
[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);
}
You are trying to call a Post method using GetAsync as well as it seems you are not passing the Maintenance object required by Rest service method as input. 您正尝试使用GetAsync调用Post方法,并且您似乎没有将Rest服务方法所需的维护对象作为输入传递。 Please try below -
请尝试以下 -
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));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.