Skip to content

Instantly share code, notes, and snippets.

@shihrer
Last active March 9, 2019 00:30
Show Gist options
  • Save shihrer/121035e1d2cc0fcd9c197962ab7d54f0 to your computer and use it in GitHub Desktop.
Save shihrer/121035e1d2cc0fcd9c197962ab7d54f0 to your computer and use it in GitHub Desktop.
.NET Core Error Message

From middleware:

return httpContext.Response.WriteAsync(new ErrorDTO
{
    Message = "Some Message",
    Title = "Some Title
}).ToString();

From Controller:

return BadRequest(new ErrorDTO
{
    Message = "Some Message",
    Title = "Some Title"
});

DTO:

public class ErrorDTO
{
    public string Message { get; set; }
    public string Title { get; set; }

    public override string ToString()
    {
        return JsonConvert.SerializeObject(this);
    }
}

Get back JSON:

{"Message":"Some Message", "Title":"Some Title"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment