1

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@domain.com",
   "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."

varunjeeri
  • 13
  • 1
  • 5

2 Answers2

1

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);
    }
/*...*/
lobi
  • 69
  • 3
-1

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.

vamsee
  • 31
  • 1
  • 9
  • 1
    `ProducesAttribute` is MVC, OP has tagged this as OData, the response from @lobi to add the `HttpPostAttribute` is correct for this common issue. – Chris Schaller Jul 12 '20 at 14:31
  • Got it. HttpPost should suffice I suppose given that application/json is a default formatter available – vamsee Oct 11 '20 at 16:22