Gaurav123
Gaurav123

Reputation: 5219

parameter is null while sending post request to webAPi from angularjs

I am trying to call one method but parameter is coming out as null

Server Side Code

[HttpPost]
        public IHttpActionResult PostRule(ActionRuleParameter actionRule)
        {
            // SOME CODE               
            return BadRequest();
        }

        public class ActionRuleParameter
        {
            public string action;
            public string rule;
        }

Client Side Code

addRule: function ($scope) {
            //var data = { "action": "post", "rule": { "ID": "1", "Name": "Ramesh", "PassFail": "Pass" } }

            var data1 = { "action": "post","rule":"rule" };


            $http({
                url: urlContent + '/api/Rules',
                method: "POST",
                data: { "actionRule": data1 }
            }).success(function (response) {
                $scope.rules = response;
            });;
        },

action and rule are coming out as null.

Upvotes: 1

Views: 1130

Answers (2)

Pankaj Parkar
Pankaj Parkar

Reputation: 136194

You need to add [FromBody] before your parameter

    public IHttpActionResult PostRule([FromBody]ActionRuleParameter actionRule)
    {
        // SOME CODE               
        return BadRequest();
    }

Upvotes: 1

Devon Burriss
Devon Burriss

Reputation: 2532

I ran in to this issue with Angular and the new ASP.NET stack. Adding [FromBody] fixed it for me so try:

 [HttpPost]
 public IHttpActionResult PostRule([FromBody]ActionRuleParameter actionRule)
 {
     // SOME CODE               
     return BadRequest();
 }

Upvotes: 0

Related Questions