WEB API OdataController POST call returns 406 Not Acceptable

1.1k Views Asked by At

When I'm sending a POST request to ODataController, record is inserted in the table, but I get the following error "The remote server returned an error: (406) Not Acceptable." GET call for the same controller works just fine.

The URL Im trying to hit is http://localhost:24419/User and JSON body for POST. I'm posting the following JSON (renamed properties):

{  
   "User_ID":0,
   "User_Name":"[email protected]",
   "First_Name":null,
   "Middle_Name":null,
   "Last_Name":null,
   "Telephone_Number":null,
   "Alternate_Email":null,
   "Record_Update_UTC_Datetime":"2019-08-06T19:42:59.5380526Z",
   "Record_Update_Application_User_Name":"username",
}

for the below model

public class User
    {
        [Key]
        public int User_ID { get; set; }
        [Required]
        [StringLength(255)]
        public string User_Name { get; set; }
        [StringLength(32)]
        public string First_Name { get; set; }
        [StringLength(32)]
        public string Middle_Name { get; set; }
        [StringLength(64)]
        public string Last_Name { get; set; }
        [StringLength(64)]
        public string Telephone_Number { get; set; }
        [StringLength(255)]
        public string Alternate_Email { get; set; }
        [Required]
        public DateTime Record_Update_UTC_Datetime { get; set; }
        [Required]
        [StringLength(128)]
        public string Record_Update_Application_User_Name { get; set; }
    }

Below is the controller:

public class UserController : ODataController
{
        [HttpGet]
        [EnableQuery(MaxExpansionDepth = 10, PageSize = 50, MaxNodeCount = 50)]
        public async Task<IHttpActionResult> GetUser()
        {
            var results = await _userService.GetUsers();
            return results.MakeWebApiActionResult(this);
        }

        public async Task<IHttpActionResult> Post([FromBody] User user)
        {
            var results = await _userService.AddUser(user);
            return Ok(results);
        }
}

I expect a 200 OK response, but the actual output is :

"ExceptionMessage": "The remote server returned an error: (406) Not Acceptable."

2

There are 2 best solutions below

0
lobi On

According to asp.net-web-api docs you need to use the [HttpPost]attribute. Try this:

public class UserController : ODataController
{
/*...*/
    [HttpPost]
    public async Task<IHttpActionResult> Post([FromBody] User user)
    {
        var results = await _userService.AddUser(user);
        return Ok(results);
    }
/*...*/
2
vamsee On

406 is specific to WebApi controllers (precisely, a response header for REST calls indicating issue in formatting output data in the desired manner) and not necessarily have to do anything with OData call made in the WebApi controller.

Please decorate your class with [Produces("application/json")] and in the request you are invoking from your APIs client, along with the requst body, also please add an "Accept" header in the request with a value of "application/json".

This should resolve your issue. Also, I hope you are not playing around with the mvc configuration services wrt Formatters of MVCServices in your startup.cs file.