我的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参数没有被填充 - 它有默认的空字符串和假布尔值。
有什么想法吗?我确实想过尝试this,但我认为对我的Web API控制器的JSON调用是可以的。
期待您的回复。
中号
更新
我已经像这样修改了我的电话,但它仍然不起作用:
$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值。为什么呢?
答案 0 :(得分:0)
实际上再看看,你需要这样的东西
$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
},
});
答案 1 :(得分:0)
请参阅更新的代码:
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;
};
在控制器中使用此服务:希望您知道如何注入服务
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....
});
希望你能从中得到帮助。
答案 2 :(得分:0)
我想我已经发现了这个问题。
在Fiddler中,它表明传递了具有值的字段,但忽略了其他字段。我必须在发送请求之前显式设置这些字段的值。我依靠模型绑定来创建字段,但我需要做额外的工作。
中号