简体   繁体   English

通过Web服务将数据发送到ASP.net MVC中的数据库

[英]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.

相关问题 将数据从 excel 发送到 ASP.NET MVC Web 服务 - sending data from excel to ASP.NET MVC web service 从ASP.net Web服务返回和发送数据 - Returning and Sending data from an ASP.net Web Service 使用ASP.NET MVC作为Web服务 - Using ASP.NET MVC as Web Service 通过ASP.NET MVC应用程序中的Ajax发布将大型JSON数据发送到操作方法中的问题 - Issue in sending large JSON data to action method through ajax post in asp.net mvc application 通过JQuery Ajax将数组发送到ASP.NET MVC模型 - Sending Array to ASP.NET MVC Model through JQuery Ajax asp.net Web服务:在json中发送json - asp.net web service : sending json within json 使用 URL 绑定来自 Web 服务的数据并将其与类绑定 - ASP.NET MVC 5 - Binding Data from web service using URL and bind it with a class - ASP.NET MVC 5 单击asp.net mvc中的按钮后,使用Web服务并通过给定的xml数据自动填充文本框 - Consuming a web service and auto populate textbox by the given xml data after clicking a button in asp.net mvc ASP.NET MVC 2和FormsAuthentication的Web服务身份验证 - Web-service authentication from ASP.NET MVC 2 and FormsAuthentication 针对外部Web服务的ASP.NET MVC Forms身份验证 - ASP.NET MVC Forms authentication against external web service
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM