ASP.net 5 Web API Post CreatedAtRoute always returns 500 Internal Server Error

The database is running. It really inserts a new record, but when I use CreateAtRoute (), I always get 500 back from the client. Why?

My controller Get:

[Route("api/[controller]")] public class IngredientController : Controller { private SimpleCookbookDbContext db { get; set; } public IngredientController(SimpleCookbookDbContext context) { db = context; } // GET: api/values [HttpGet] public async Task<IEnumerable<Ingredient>> Get() { return await db.Ingredients.ToListAsync(); } // GET api/values/5 [HttpGet("{id}", Name = "GetIngredient")] public async Task<Ingredient> Get(int id) { return await db.Ingredients.SingleOrDefaultAsync(i => i.Id == id); } [HttpPost] public async Task<IActionResult> Post([FromBody]Ingredient ingredient) { try { var res = await IM.CreateAsync(ingredient); if (!res.Success) { return HttpBadRequest(res.Errors); } } catch(Exception) { return new HttpStatusCodeResult((int)HttpStatusCode.InternalServerError); } return CreatedAtRoute("GetIngredient", new { controller="Ingredient", id = ingredient.Id }); } } 

I tried to debug this. Yes, it will return an HttpBadRequest if the ingredient I'm trying to insert already exists.

I tried to set a breakpoint inside the catch block and I am not getting there, so I assume there were no errors in the database.

The record will be inserted into the database. I get to the line return CreatedAtRoute(...); but getting 500 back. (I also set a breakpoint there).

Now I am using a violinist. My request is as follows:

 POST /api/ingredient HTTP/1.1 Host: localhost:55303 Content-Type: application/json;charset=utf-8 {"id":0, "name": "rosemary", "description": "rosemary"} 

I also removed the double quotes in the property names, and I still get the same 500.

I have a camel body allowed at startup:

 services.AddMvc().AddJsonOptions(options => { options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); }); 

I think I showed all the relevant code. If you need more, please let me know.


UPDATE

CreatedAtRoute has an overload with three parameters:

 return CreatedAtRoute("GetIngredient", new { controller="Ingredient", id = ingredient.Id }, ingredient); 

The last parameter is an object that you can create dynamically or transfer your entire object depending on what you want to open.

It is strange, as there is a 2-parameter variant, which will lead to a strange answer of 500.

+7
asp.net-web-api asp.net-core-mvc
source share

No one has answered this question yet.

See related questions:

1153
How to force ASP.NET Web API to return JSON instead of XML using Chrome?
392
WCF vs ASP.NET Web API
372
How to protect ASP.NET web API
365
Pass an array of integers to the ASP.NET Web API?
296
ServiceStack vs ASP.Net Web API
289
Returning a binary file from a controller in ASP.NET Web API
250
Best Practice for Error Returns in ASP.NET Web API
63
ASP.NET Web API: 500 non-descriptive internal server error
4
C # Web API - Internal Server Error 500
0
MVC Web API POST Entity (500 internal server errors)

All Articles