0

I am getting post as null in My API controller Action GetVoteScores. Where i went wrong?. If any one need more code to check i will give it.

This is my API controller Action

    [System.Web.Mvc.HttpGet]
    [System.Web.Mvc.ActionName("GetVoteScores")]
    public string GetVoteScores(int id, int UserId, int IsUpVote,Post post)
    {
        return "1";
    }

This is my WebApI.config file

     config.Routes.MapHttpRoute(
           name: "MyRoute",
           routeTemplate: "api/{controller}/GetVoteScores/{id}/{UserId}/{IsUpVote}",
           defaults: new { id = RouteParameter.Optional, UserId = RouteParameter.Optional, IsUpVote = RouteParameter.Optional }
       );

And finally my Ajax call look like

   var post = new Post();
    post.PostId = self.PostId;
    post.PostedBy = self.PostedBy;

    return $.ajax({
        url: postApiUrl + "GetVoteScores/" + self.PostId + "/1/0",
        dataType: "json",
        contentType: "application/json",
        cache: false,
        type: 'GET',
        data: ko.toJSON(post)
    })
Nithin Paul
  • 2,169
  • 3
  • 33
  • 55
  • How is `Post` looks like? – haim770 Nov 06 '14 at 08:27
  • Mu URL looks like http://localhost:1268/api/WallPost/GetVoteScores/1/1/0?{%22PostId%22:1,%22Message%22:%22Sample%20Text%22,%22PostedBy%22:1,%22PostedByName%22:%22Nidhin%22,%22PostedByAvatar%22:%22/Images/profileimages/user.png%22,%22PostedDate%22:null,%22VoteScore%22:5,%22PostComments%22:[],%22PostDateFormat%22:%22%22}&_=1415262533319 – Nithin Paul Nov 06 '14 at 08:32
  • You need to change `type` to `POST` instead of `GET`. – haim770 Nov 06 '14 at 08:33
  • haim770 i changed it to type post. But my API controller have another function which named "PostPost". Now the call is going to that function not to GetVoteScores – Nithin Paul Nov 06 '14 at 08:36
  • Change `[System.Web.Mvc.HttpGet]` to `[System.Web.Mvc.HttpPost]` as well. – haim770 Nov 06 '14 at 08:37
  • Yes i changed it already. Do i need to change my function name from GetVoteScores to PostVoteScore. Is there is any relation with function name in API calls? – Nithin Paul Nov 06 '14 at 08:38
  • You aren't supposed to send complex data types (usually as JSON) to `GET` actions. If you only need to pass `PostId` and `PostedBy`, try to attach it to the Query-string as well. – haim770 Nov 06 '14 at 08:40
  • Ok but i need to send my "post" object too. – Nithin Paul Nov 06 '14 at 08:41

0 Answers0