Web API 2 REST Get() not receiving parameters: Ang

2019-09-19 10:52发布

问题:

I am having a problem with my REST Get() method. I am calling it with parameters from my AngularJS controller but the id parameter is not being populated correctly.

Here is my Web API 2 controller:

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;
}

Here is my Angular call:

$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
    });
}

Here is my routing config:

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 }
    );
}

The problem is that the id parameter is not being populated - it has default empty strings and false booleans.

Any ideas? I did think of trying this but I think JSON calls to my Web API controller are ok.

Looking forward to your responses.

M

UPDATE

I have modified my call like this, but it still doesn't work:

$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 showing some interesting results **

I have modified my code like this:

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

but Fiddler reports that only the CodeX values are being passed. Why?

回答1:

Actually taking another look, you need something like this

    $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
                },
});


回答2:

See the updated code:

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;
}

Angular Call:

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

use this service in controller: Hope you know how to inject service

  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....
  });

Hope you can get help from this.



回答3:

I think I have discovered the problem.

In Fiddler, it shows that fields that have a value are passed, but the others are ignored. I must explicitly set the values of these fields before sending the request. I was relying on model binding to create the fields but I need to do smoe additional work.

M