即从AngularJS发送JSON对象到ASP.NET的WebAPI的名称相匹配的控制器上是如何解决这个问题,没有发现行动

问题描述 投票:0回答:1

我需要通过在AngularJS前端的WebAPI输入的数据和检索另一组数据来填充网格上。我试图通过数据作为JSON对象的WebAPI方法。在的WebAPI的方法,所述参数我传递的JSON对象作为一类对象。

我不能够进入到特定的web API方法时,我使用[HTTPPost]和得到错误的,没有行动的请求匹配的控制器上找到。

但我能够进入到具有[HTTPGET]其他Web API方法。

可有人请告知如何解决issue.Thanks!

的WebAPI

using System.Web.Http;
using AttributeRouting.Web.Http;

namespace webAPITestProject.Controllers
{
    [Route("api/Values")] 
    public class ValuesController : ApiController
    {
        retrieveEmployeeData empData = new retrieveEmployeeData();
        retrieveProductDetails prodDetls = new retrieveProductDetails();    

        [Route("PostEmployeeData")] 
        [HttpPost]
        public DataTable PostEmployeeData([FromBody] Employer empDetails)
        {
            DataTable dataTable = new DataTable { TableName = "MyTableName" };
            dataTable = empData.getEmployeeData(empDetails);
            return dataTable;
        }
    }
}

AngularJS - 控制器

angular.module('Test.Employer')
    .controller('EmployerController', ['$scope','headerValue', '$http',
        function ($scope, headerValue, $http) { 
            var ipEmployerDetls = {                   
                EmployerName: "cherokee",
                Company: "ABC"                   
            };

            $http({ 
                url: 'http://localhost:53583/api/Values/PostEmployeeData?empDetails='+'"'+JSON.stringify(ipEmployerDetls)+'"',
                dataType: 'json', 
                method: 'POST', 
                data: JSON.stringify(ipEmployerDetls), 
                headers: { 
                    "Content-Type": "application/json" 
                } 
            }).success(function (response) { 
                $scope.object = response.data;
            }) 
                .error(function (error) { 
                    alert(error.Message); 
                });
})();

雇主类

public class Employer
{
    public string Company{get;set;}
    public string EmployerName{get;set;}
}
angularjs asp.net-web-api
1个回答
0
投票

您发布的数据到端点,您不需要在URL中添加任何东西。

  $http({ 
                url: 'http://localhost:53583/api/Values/PostEmployeeData',
                dataType: 'json', 
                method: 'POST', 
                data: JSON.stringify(ipEmployerDetls), 
                headers: { 
                    "Content-Type": "application/json" 
                } 
            }).success(function (response) { 
                $scope.object = response.data;
            }) 
                .error(function (error) { 
                    alert(error.Message); 
                });

我已经清除了你的电话,你提供的数据报文的发送正文,且您的控制器有期望它由[FromBody]标签所示

© www.soinside.com 2019 - 2024. All rights reserved.