Skip to content

Instantly share code, notes, and snippets.

@khalidabuhakmeh
Created January 1, 2019 17:08
Show Gist options
  • Save khalidabuhakmeh/0993dbb41e15a495daf600d6d373b90f to your computer and use it in GitHub Desktop.
Save khalidabuhakmeh/0993dbb41e15a495daf600d6d373b90f to your computer and use it in GitHub Desktop.
Carter Tweaks
// ctx is a CarterContext<T> where T is UpdateActor
// this allows the context to run binding and validation
// ctx also has property accessors for Request, Response, and other helpers.
this.Put<UpdateActor>("/actors/{id:int}", async (ctx) =>
{
// validation is called once and results are cached for request
if (!ctx.IsValid)
{
ctx.Response.StatusCode = 422;
await res.Negotiate(ctx.GetFormattedErrors());
return;
}
var actor = db.Actors.Find(ctx.Model.Id);
ctx.Bind(actor);
await db.SaveChangesAsync();
ctx.Response.StatusCode = 204;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment