0

I am building a rest API with asp.net my problem is that when I try to add a student to my database like that :

http://localhost:50001/api/Students?&FirstName=cc&LastName=cc&[email protected]&DropOut=false&Live=false&ClassId=1&ImageId=1

I get "the value variable is null", this is my code to add a student:

      // Get All Students
        [Route("api/Students")]
        public IEnumerable<Student> Get()
        {
            return _StudentService.Queryable().ToList();
        }
// Insert Student
[Route("api/Students/")]
public IEnumerable<Student> Post(Student value)
        {
              cc.Students.Add(value);
                    cc.SaveChanges();
                    return Get();
   }

I have used "Fiddler web Debugger" to test my URLs an it works only in this way:

enter image description here

now If I have an angularJS client that tries to add a new student to the database,how can I send data as a json format in an URL

this is how I add a new student from my client angularJS:

$http({method: 'POST', url: 'http://localhost:50001/api/Students?&FirstName=cc&LastName=cc&[email protected]&DropOut=false&Live=false&ClassId=1&ImageId=1})

                 .success(function (data) {
                     console.log("success");

         }).error(function (data, status, headers, config) {

                     console.log("data error ...");
                            });

thanks a lot for help

4
  • 1
    If you need to send data in a JSON format, why not use POST? Otherwise, you can use JSON.stringify(). Commented Oct 2, 2015 at 19:33
  • ok @jason9187 thanks I will test JSON.stringify() because I already use POST in my angularJS client Commented Oct 2, 2015 at 19:37
  • 1
    But if you stringify the contents, then you'll just have to parse it back out on the back end. I would suggest you stick with POST. Commented Oct 2, 2015 at 19:40
  • ok jason thanks a lot :) Commented Oct 2, 2015 at 19:46

1 Answer 1

3

If you are saying you want a true Rest API you should continue to use the POST verb as it is more semantically right for creating a new student.

Passing a new student on the URL is possible but not in the configuration you have provided. Your API method expects a POST request and that the new student be located in the HTTP body.

Just configure your angular call to use jsonData and post it to your API.

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.