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; }
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.