1

This is my models class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Test.Models
{
    public class User
    {
        public int Id { get; set; }
        public string Name { get; set; }
        //public DateTime startTime { get; set; }
        //public DateTime endTime { get; set; }
        //public int Age { get; set; }
        //public string Adress { get; set; }
    }
}

This is my controller class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Test.DBA;
using Test.Models;

namespace Test.Controllers
{
    public class UserAPIController : ApiController
    {
        ApiDbContext dbContext = null;
        public UserAPIController()
        {
            dbContext = new ApiDbContext();
        }
        [HttpPost]
        public IHttpActionResult InsertUser(User user)
        {
            dbContext.Users.Add(user);
            dbContext.SaveChangesAsync();

            return Ok(user.Name);
        }

        public IEnumerable<User> GetAllUser()
        {
            var list = dbContext.Users.ToList();
            return list;
        }

        [HttpPost]
        public IHttpActionResult DeleteUser(User user)
        {
            dbContext.Users.Remove(user);
            dbContext.SaveChanges();

            return Ok(user.Name);
        }

        [HttpGet]
        public IHttpActionResult ViewUser(int id)
        {
            var student = dbContext.Users.Find(id);
            return Ok(student);
        }

        [HttpPost]
        public IHttpActionResult UpdateUser(User user)
        {
            User std = dbContext.Users.Find(user.Id);

            std.Name = user.Name;
            //std.startTime = user.startTime;
            //std.endTime = user.endTime;
            //std.Age = user.Age;
            //std.Adress = user.Adress;

            dbContext.Entry(std).State = System.Data.Entity.EntityState.Modified;
            dbContext.SaveChangesAsync();

            return Ok();
        }
    }
}

I'm trying to call the post method with the google chrome app Postman. I call it with raw JSON body:

{
    Id : 1,
    Name : "Sample"
}

The get methods work, but when i debug post methods the parameters are always null.

Edit 1: Routing:

config.Routes.MapHttpRoute(
              name: "DefaultApi",
              routeTemplate: "api/{controller}/{action}/{id}",
              defaults: new { id = RouteParameter.Optional }
          );
7
  • What type of routing are you using? Commented Nov 7, 2016 at 10:42
  • I edited the original post. Commented Nov 7, 2016 at 10:44
  • What do you use for Accept and Content-Type headers? Commented Nov 7, 2016 at 10:51
  • 1
    @foobar Sorry I'm new to this I'm not sure what you're asking but if you're asking about the post request I don't use any headers, and I don't know what accept is Commented Nov 7, 2016 at 10:52
  • @Saizaku, yes, these are request headers. I think postman takes care of these headers behind the scene for you. However this sometimes causes issues. Commented Nov 7, 2016 at 10:55

2 Answers 2

1

Try adding the following request header to postman:

Content-Type: application/json
Sign up to request clarification or add additional context in comments.

3 Comments

Don't forget that the radio button for "raw" should then be selected in the Body tab.
Thank you it worked, I was trying to get this to work for a couple of days, thank you so much!
@toadfladkx I already had that radio button selected, just missing the header
1

Try decorating your parameters with [FromBody]:

public IHttpActionResult DeleteUser([FromBody]User user)

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.