网页API 2 REST的get()不接收参数:角 [英] Web API 2 REST Get() not receiving parameters: Angular
问题描述
我有我的REST get()方法的一个问题。我从我的AngularJS控制器,但id参数没有被正确填充参数调用它。
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.
下面是我的Web API 2控制器:
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
});
}
下面是我的路由配置:
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参数 - 它默认空字符串和假布尔值
The problem is that the id parameter is not being populated - it has default empty strings and false booleans.
任何想法?我没有想到尝试这个一>但我认为JSON调用我的Web API控制器都OK了。
Any ideas? I did think of trying this but I think JSON calls to my Web API controller are ok.
期待您的回复。
M
更新
我已经修改我的电话是这样,但它仍然无法正常工作:
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 **
我已经修改我的code是这样的:
I have modified my code like this:
$http({
method: 'POST',
url: '/api/customer',
data: id,
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
});
但菲德勒报道说,只有codeX值被传递。为什么呢?
but Fiddler reports that only the CodeX values are being passed. Why?
推荐答案
我想我已经发现了这个问题。
I think I have discovered the problem.
在提琴手,这表明具有值字段都通过了,但其他被忽略。我必须在发送请求之前明确设置这些字段的值。我靠模型绑定创建字段,但我需要做的SMOE额外的工作。
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
这篇关于网页API 2 REST的get()不接收参数:角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!