【问题标题】:Web API 2 REST Get() not receiving parameters: AngularWeb API 2 REST Get() 未接收参数:Angular
【发布时间】:2015-06-16 01:24:54
【问题描述】:

我的 REST Get() 方法有问题。我使用 AngularJS 控制器中的参数调用它,但没有正确填充 id 参数。

这是我的 Web API 2 控制器:

public IEnumerable<string> Get(SearchParameters id)
{
    // not important at the moment
    return null;
}

public struct SearchParameters
{
    string selectedBroker;
    string brokerIsUnallocated;
    string brokerIncludeDeleted;
    string customerId;
    string businessType;
    string companyName;
    string town;
    string department;
    string contactName;
    string country;
    bool codeP;
    bool codeC;
    bool codeT;
    bool codeS;
    bool codeX;
    bool codeD;
}

这是我的 Angular 调用:

$scope.search = function() {
    $http.get("/api/customer", {
        selectedBroker: $scope.selectedBroker,
        brokerIsUnallocated: $scope.brokerIsUnallocated,
        brokerIncludeDeleted: $scope.brokerIncludeDeleted,
        customerId: $scope.customerCustomerId,
        businessType: $scope.customerBusinessType,
        companyName: $scope.customerCompanyName,
        town: $scope.customerTown,
        department: $scope.selectedDepartment,
        contactName: $scope.customerContactName,
        country: $scope.selectedCountry,
        codeP: $scope.codeP,
        codeC: $scope.codeC,
        codeT: $scope.codeT,
        codeS: $scope.codeS,
        codeX: $scope.codeX,
        codeD: $scope.codeD
    });
}

这是我的路由配置:

public static void Register(HttpConfiguration config)
{
    // Web API configuration and services
    // Web API routes
    config.MapHttpAttributeRoutes();

    config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { id = RouteParameter.Optional }
    );
}

问题在于没有填充 id 参数 - 它具有默认的空字符串和 false 布尔值。

有什么想法吗?我确实想过尝试this,但我认为对我的 Web API 控制器的 JSON 调用是可以的。

期待您的回复。

M

更新

我已经像这样修改了我的调用,但它仍然不起作用:

$scope.search = function() {
    $http({
        url: '/api/customer',
        method: 'POST',
        params:
        {
            id: {
                selectedBroker: $scope.selectedBroker,
                brokerIsUnallocated: $scope.brokerIsUnallocated,
                brokerIncludeDeleted: $scope.brokerIncludeDeleted,
                customerId: $scope.customerCustomerId,
                businessType: $scope.customerBusinessType,
                companyName: $scope.customerCompanyName,
                town: $scope.customerTown,
                department: $scope.selectedDepartment,
                contactName: $scope.customerContactName,
                country: $scope.selectedCountry,
                codeP: $scope.codeP,
                codeC: $scope.codeC,
                codeT: $scope.codeT,
                codeS: $scope.codeS,
                codeX: $scope.codeX,
                codeD: $scope.codeD
            }
        }
    });
};

** EDIT Fiddler 显示一些有趣的结果 **

我已经像这样修改了我的代码:

$http({
    method: 'POST',
    url: '/api/customer',
    data: id,
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
});

但 Fiddler 报告仅传递 CodeX 值。为什么?

【问题讨论】:

  • stackoverflow.com/questions/13760070/… - 我也有这个问题,请查看链接
  • 如果要从api获取数据????你为什么要使用 $http.get("/api/customer", { selectedBroker: $scope.selectedBroker,............
  • 感谢 Ric 的链接,但它并没有真正的帮助。该帖子的回复对我不起作用。
  • 您可能需要做的不仅仅是帖子,请尝试先创建对象,然后通过params : { id : object } 等发送它

标签: c# angularjs asp.net-web-api asp.net-web-api2


【解决方案1】:

其实换个角度看,你需要这样的东西

    $http({
                url: '/api/customer/',
                method: 'GET',
                params: {
                    selectedBroker: $scope.selectedBroker,
                    brokerIsUnallocated: $scope.brokerIsUnallocated,
                    brokerIncludeDeleted: $scope.brokerIncludeDeleted,
                    customerId: $scope.customerCustomerId,
                    businessType: $scope.customerBusinessType,
                    companyName: $scope.customerCompanyName,
                    town: $scope.customerTown,
                    department: $scope.selectedDepartment,
                    contactName: $scope.customerContactName,
                    country: $scope.selectedCountry,
                    codeP: $scope.codeP,
                    codeC: $scope.codeC,
                    codeT: $scope.codeT,
                    codeS: $scope.codeS,
                    codeX: $scope.codeX,
                    codeD: $scope.codeD
                },
});

【讨论】:

  • 不幸的是,这不起作用,但我们已经很接近了:我能在我的水中感觉到它。
  • 尝试将data:更改为params:
  • 我打败了你。我在另一篇文章中看到了这个建议,但它仍然不起作用。
  • API 是否从这些参数中接收到任何信息?
  • 在将对象发送到paramsvar obj = { selectedBroker: $scope.selectedBroker }; 之前尝试构造对象
【解决方案2】:

查看更新后的代码:

public List<SearchParameters> Get(SearchParameters id)
{
// not important at the moment
 return null;
}

public struct SearchParameters
{
string selectedBroker;
string brokerIsUnallocated;
string brokerIncludeDeleted;
string customerId;
string businessType;
string companyName;
string town;
string department;
string contactName;
string country;
bool codeP;
bool codeC;
bool codeT;
bool codeS;
bool codeX;
bool codeD;
}

角度调用:

app.service("angularService", function ($http) {    
   $scope.search = function(id) {
    var response = $http({
        method: "get",
        url: "api/customer",
        params: {
            id:id
        }
    });
    return response;
};

在控制器中使用这个服务:希望你知道如何注入服务

  app.controller("editController", function ($scope,$routeParams,   angularService) {
  angularService.search(id).then(function (response) {
               $scope.selectedBroker = response.data[0].selectedBroker, 
          $scope.brokerIsUnallocated= response.data[0].brokerIsUnallocated, 
          $scope.brokerIncludeDeleted =response.data[0].brokerIsUnallocated, 
         //similarly do for all property....
  });

希望你能从中得到帮助。

【讨论】:

  • 感谢您对@Reena 的广泛回复,但我认为您的服务代码格式错误。我打算将代码移动到服务中,但我想让它首先内联工作。 id 参数是搜索条件,目前我对服务返回的内容不感兴趣;我只是担心 .NET Web API 调用接收参数。
  • 应用断点并检查它是否命中您的方法。并且也以这种方式使用并检查它是否在此处遇到断点 public List Get(SearchParameters id)
【解决方案3】:

我想我已经发现了问题。

在 Fiddler 中,它显示有值的字段被传递,但其他的被忽略。在发送请求之前,我必须明确设置这些字段的值。我依靠模型绑定来创建字段,但我需要做一些额外的工作。

M

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-07
    • 1970-01-01
    • 2017-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多