0

I have a simple WebApi that I'd like to connect to an angularjs app

The api looks something like this

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

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

Thanks

2
  • Have you tried just using SokApi.query({ "Limit": 10, "Offset": 0 }); Commented Jan 26, 2015 at 11:54
  • @WayneEllery Yea, same result Commented Jan 26, 2015 at 11:58

1 Answer 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:

public HttpResponseMessage GetSokAvtal([FromUri] SokAvtalAdvanced sokAvtalAdvanced)
{
    ...
}

Also on the client you should use:

$scope.avtal = SokApi.query({
    "Limit": 10,
    "Offset": 0
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.