I have a simple WebApi that I'd like to connect to an angularjs app
我有一个简单的WebApi,我想连接到angularjs应用程序
The api looks something like this
api看起来像这样
public HttpResponseMessage GetSokAvtal(SokAvtalAdvanced sokAvtalAdvanced)
{
if (sokAvtalAdvanced != null && sokAvtalAdvanced.IsValid())
{
try
{
var avtal = db.spSokAvtalAdvanced(
limit: sokAvtalAdvanced.Limit,
offset: sokAvtalAdvanced.Offset);
return Request.CreateResponse(HttpStatusCode.OK, avtal);
}
catch (Exception ex)
{
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Ett fel har inträffat");
}
}
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Felaktiga parametrar");
}
The SokAvtalAdvanced looks like this
SokAvtalAdvanced看起来像这样
public class SokAvtalAdvanced
{
#region Properties
private int _limit;
public int Limit
{
get { return _limit > 0 && _limit < 50 ? _limit : 10; }
set { _limit = value; }
}
private int _offset;
public int Offset
{
get { return _offset > 0 ? _offset : 0; }
set { _offset = value; }
}
#endregion
#region Methods
/// <summary>
/// Returns a valid modelstate
/// </summary>
/// <returns></returns>
public bool IsValid()
{
return true;
}
#endregion
}
To map this to angular I've done this so far without any luck
为了将这个映射到有角度我到目前为止没有任何运气
app.controller("SokAvtalController", ['$scope', 'SokApi', function ($scope, SokApi) {
$scope.avtal = SokApi.query({ sokAvtalAdvanced: {
"Limit": 10,
"Offset": 0
}});
console.log($scope.avtal);
$scope.SelectGroup = function (avtal) {
};
}]);
angular.module('app.avtalsportalen', ['ngResource'])
.factory('SokApi', ['$resource', function ($resource) {
return $resource('/api/SokAvtal/:sokAvtalAdvanced');
}]);
Any idea of what's wrong with my call? The SokAvtalAdvanced sokAvtalAdvanced
is null in WebApi every call
我知道我的电话有什么问题吗?每次调用时,WebApi中的SokAvtalAdvanced sokAvtalAdvanced为空
Thanks
1 个解决方案
#1
1
Since this is a GET request you need to indicate the model binder to read from the url by using the [FromUri]
attribute:
由于这是一个GET请求,您需要使用[FromUri]属性指示要从URL读取的模型绑定器:
public HttpResponseMessage GetSokAvtal([FromUri] SokAvtalAdvanced sokAvtalAdvanced)
{
...
}
Also on the client you should use:
另外在客户端上你应该使用:
$scope.avtal = SokApi.query({
"Limit": 10,
"Offset": 0
});
#1
1
Since this is a GET request you need to indicate the model binder to read from the url by using the [FromUri]
attribute:
由于这是一个GET请求,您需要使用[FromUri]属性指示要从URL读取的模型绑定器:
public HttpResponseMessage GetSokAvtal([FromUri] SokAvtalAdvanced sokAvtalAdvanced)
{
...
}
Also on the client you should use:
另外在客户端上你应该使用:
$scope.avtal = SokApi.query({
"Limit": 10,
"Offset": 0
});