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?