title: Return custom error message in ASP.NET Core Web Api
body: <p>I am writing a webapi in <code>ASP.NET CORE 1.1</code>. I am trying to figure out how to return error message with not acceptable status code <code>406</code>.</p>
  
  <p>Here is the method I have written in one of the Api controller:</p>
  
  <pre><code>[HttpPost]
      public async Task&lt;IActionResult&gt; PostStoreCoolerData([FromBody] StoreCoolerData storeCoolerData)
      {
          if (!ModelState.IsValid)
          {
              return BadRequest(ModelState);
          }
  
          _context.StoreCoolerData.Add(storeCoolerData);
          var addResponse = await _context.SaveChangesAsync();
          if (addResponse &gt; 0)
          {
              var getItemInfo = _context.StoreCoolerData.Include(c =&gt; c.CoolerBrand)
                                                        .Include(c =&gt; c.CoolerType)
                                                        .Include(c =&gt; c.DrinkType)
                                                        .Where(i =&gt; i.Id == storeCoolerData.Id);
  
              return CreatedAtAction("GetStoreCoolerData", new { getItemInfo });
          }
          return StatusCode(406, "There was a problem saving record in the database. Please try again.");
      } 
  </code></pre>
  
  <p>As you can see I have <code>return StatusCode(406, "There was a problem saving record in the database. Please try again.");</code> at the end of my code. When I run the code and try to get response back with custom error, I am not getting custom error message rather I am getting Not Acceptable message. </p>
  
  <p>Not sure if this is the right way to do it. Is there any other way I can include custom error message that is not 200 > or &lt; 400?</p>
  
  <p>Any help is really appreciated.</p>
  
  <p><strong>UPDATE 1</strong></p>
  
  <p><a href="https://i.stack.imgur.com/d11vK.png" rel="noreferrer"><img src="https://i.stack.imgur.com/d11vK.png" alt="enter image description here"></a></p>
  
  <p>Thanks,</p>
  
  <p>Ray</p>