Skip to content

Instantly share code, notes, and snippets.

@judah4
Created March 1, 2019 19:13
Show Gist options
  • Save judah4/c3822e48c5c4b8ef4ff54ce4899e83f6 to your computer and use it in GitHub Desktop.
Save judah4/c3822e48c5c4b8ef4ff54ce4899e83f6 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
namespace Gnarlywood.Core.Sites
{
public static class ActionResultHelper
{
public static IActionResult Json(object content, int? statusCode = null)
{
return new ContentResult() {Content = JsonConvert.SerializeObject(content), ContentType = "application/json", StatusCode = statusCode};
}
/// <summary>
/// https://tools.ietf.org/html/rfc7807 problem response
/// </summary>
/// <param name="problem"></param>
/// <param name="statusCode"></param>
/// <returns></returns>
public static IActionResult Problem(ProblemBase problem, int statusCode = 400)
{
return new ContentResult() { Content = JsonConvert.SerializeObject(problem), ContentType = "application/problem+json", StatusCode = statusCode };
}
public static IActionResult NotFoundProblem(string missingType, string instance = null)
{
return Problem(new ProblemBase()
{Title = "Not Found", Type = missingType, Description = $"Object of Type {missingType} was not found", Instance = instance}, 404);
}
}
}
using System;
using Newtonsoft.Json;
namespace Gnarlywood.Core.Sites
{
public class ProblemBase
{
[JsonProperty("title")]
public string Title { get; set; }
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("description")]
public string Description { get; set; }
[JsonProperty("instance", DefaultValueHandling = DefaultValueHandling.Ignore)]
public string Instance { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment