Fillah
Fillah

Reputation: 293

Parse Json object from Angular to C# ASP.Net Core Web API

I'm trying to create a new Salesman with Angular and C#. From Angular i collect the data the user has typed into an array (newData) and sending it from my controller --> service to my C# controller server-side. But i get several errors and it can't get my object.

Angular controller:

$scope.addSalesman = function (newData) {
    myService.addNewSalesman(newData).then(function (data) {
      console.log(data);
    }, function (err) {
      console.log(err);
    });
  };

Angular service:

addNewSalesman: function (newData) {
            var deferred = $q.defer();
            $http({
                method: 'POST',
                url: '/api/Salesman',
                headers: { 'Content-type': 'application/json' }
            }, newData).then(function (res) {
                deferred.resolve(res.data);
            }, function (res) {
                deferred.reject(res);
            });
            return deferred.promise;
        }

C# controller:

public HttpResponseMessage Post([FromBody] newData newdata) {
            return Request.CreateResponse(HttpStatusCode.OK);
        }

My errors are on the C# controller:

The type or namespace "newData" could not be found

"HttpRequest" does not contain a definition for "CreateResponse" accepting first argument of type "HttpRequest"

I tried adding the using System.Net.Http; and using System.Net; but doesn't work. Any suggestions?

Upvotes: 1

Views: 2276

Answers (3)

Sigge
Sigge

Reputation: 2172

You are getting two compile time errors which don't really have anything to do with one another.

1. The type or namespace "newData" could not be found

Is caused because your parameter type "newData" is not a known type in your code. Say you create a class like

public class Salesman
{
    public long Id { get; set; }
    public string Name { get; set; }
}

and the javascript object is

var salesman = {
    id = 2,
    name = "Peter Sellers"
};

Now when you're passing this object in, for example by using $http.post

var res = $http.post('/api/addsalesman', salesman);
res.success(function (data, status, headers, config) {
    alert(data);
});
res.error(function (data, status, headers, config) {
    alert('error');
}); 

then the following Controller method would be able to parse it.

[Route("/api/addsalesman")]
[HttpPost]
public IActionResult AddSalesman([FromBody] Salesman salesman)
{

}

2. "HttpRequest" does not contain a definition for "CreateResponse" accepting first argument of type "HttpRequest"

The method CreateResponse() doesn't exist for this.Request. Anyways, I would suggest returning an object, which would be automatically serialized. Alternatively, you could return a not found response resulting in 404, or even throw an Exception, resulting in Statuscode 500.

[Route("/api/addsalesman")]
[HttpPost]
public IActionResult AddSalesman([FromBody] Salesman salesman)
{
    //Do Stuff
    if (stuffNotOk)
    {
        return NotFound();
    }
    return Ok(product); 
}

Upvotes: 2

Anton Khramov
Anton Khramov

Reputation: 11

The type or namespace "newData" could not be found

This means that class newData should be created which reflect data passed by UI

"HttpRequest" does not contain a definition for "CreateResponse" accepting first argument of type "HttpRequest"

Try this

Upvotes: 0

Nikolaj Dam Larsen
Nikolaj Dam Larsen

Reputation: 5674

It looks like you may have misspelled the name of the argument type you expect in your Post method:

//                                         v-- this is your sinner
public HttpResponseMessage Post([FromBody] newData newdata) {
    return Request.CreateResponse(HttpStatusCode.OK);
}

Upvotes: 0

Related Questions