【发布时间】: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