1+ using System ;
2+ using System . Net ;
3+ using Microsoft . AspNetCore . Mvc ;
4+ using Newtonsoft . Json ;
5+
6+ namespace Skybrud . Essentials . AspNetCore . Models . Json {
7+
8+ /// <summary>
9+ /// Class representing a JOSN based result serialized using <strong>Newtonsoft.Json</strong>.
10+ /// </summary>
11+ [ Obsolete ( "Use the 'NewtonsoftJsonNetResult' class instead." ) ]
12+ public class JsonNetResult : ContentResult {
13+
14+ #region Constructors
15+
16+ /// <summary>
17+ /// Initializes a new <strong>200 OK</strong> result wrapping the specified <paramref name="value"/>.
18+ /// </summary>
19+ /// <param name="value">The value/body of the result. The value will be serialized to JSON before being returned to the client.</param>
20+ public JsonNetResult ( object value ) : this ( HttpStatusCode . OK , value ) { }
21+
22+ /// <summary>
23+ /// Initializes a new result with <paramref name="status"/> wrapping the specified <paramref name="value"/>.
24+ /// </summary>
25+ /// <param name="status">The HTTP status code.</param>
26+ /// <param name="value">The value/body of the result. The value will be serialized to JSON before being returned to the client.</param>
27+ public JsonNetResult ( HttpStatusCode status , object value ) {
28+
29+ // Serialize the data to a JSON string using JSON.net
30+ string json = JsonConvert . SerializeObject ( value , Formatting . None ) ;
31+
32+ StatusCode = ( int ) status ;
33+ ContentType = "application/json" ;
34+ Content = json ;
35+
36+ }
37+
38+ #endregion
39+
40+ #region Static methods
41+
42+ /// <summary>
43+ /// Returns a new <strong>200 OK</strong> result with the specified <paramref name="value"/>.
44+ /// </summary>
45+ /// <param name="value">The value/body of the result. The value will be serialized to JSON before being returned to the client.</param>
46+ /// <returns>An instance of <see cref="JsonNetResult"/>.</returns>
47+ public static JsonNetResult Ok ( object value ) {
48+ return new JsonNetResult ( HttpStatusCode . OK , value ) ;
49+ }
50+
51+ /// <summary>
52+ /// Returns a new <strong>201 Created</strong> result with the specified <paramref name="value"/>.
53+ /// </summary>
54+ /// <param name="value">The value/body of the result. The value will be serialized to JSON before being returned to the client.</param>
55+ /// <returns>An instance of <see cref="JsonNetResult"/>.</returns>
56+ public static JsonNetResult Created ( object value ) {
57+ return new JsonNetResult ( HttpStatusCode . Created , value ) ;
58+ }
59+
60+ /// <summary>
61+ /// Returns a new <strong>400 Bad Request</strong> result with the specified <paramref name="error"/> message.
62+ /// </summary>
63+ /// <param name="error">A message describing the error.</param>
64+ /// <returns>An instance of <see cref="JsonNetResult"/>.</returns>
65+ public static JsonNetResult BadRequest ( string error ) {
66+ JsonBody body = new ( HttpStatusCode . BadRequest , error , null ) ;
67+ return new JsonNetResult ( HttpStatusCode . BadRequest , body ) ;
68+ }
69+
70+ /// <summary>
71+ /// Returns a new <strong>401 Unauthorized</strong> result with the specified <paramref name="error"/> message.
72+ /// </summary>
73+ /// <param name="error">A message describing the error.</param>
74+ /// <returns>An instance of <see cref="JsonNetResult"/>.</returns>
75+ public static JsonNetResult Unauthorized ( string error ) {
76+ JsonBody body = new ( HttpStatusCode . Unauthorized , error , null ) ;
77+ return new JsonNetResult ( HttpStatusCode . Unauthorized , body ) ;
78+ }
79+
80+ /// <summary>
81+ /// Returns a new <strong>403 Forbidden</strong> result with the specified <paramref name="error"/> message.
82+ /// </summary>
83+ /// <param name="error">A message describing the error.</param>
84+ /// <returns>An instance of <see cref="JsonNetResult"/>.</returns>
85+ public static JsonNetResult Forbidden ( string error ) {
86+ JsonBody body = new ( HttpStatusCode . Forbidden , error , null ) ;
87+ return new JsonNetResult ( HttpStatusCode . Forbidden , body ) ;
88+ }
89+
90+ /// <summary>
91+ /// Returns a new <strong>404 Not Found</strong> result with the specified <paramref name="error"/> message.
92+ /// </summary>
93+ /// <param name="error">A message describing the error.</param>
94+ /// <returns>An instance of <see cref="JsonNetResult"/>.</returns>
95+ public static JsonNetResult NotFound ( string error ) {
96+ JsonBody body = new ( HttpStatusCode . NotFound , error , null ) ;
97+ return new JsonNetResult ( HttpStatusCode . NotFound , body ) ;
98+ }
99+
100+ /// <summary>
101+ /// Returns a new <strong>500 Internal Server Error</strong> result with the specified <paramref name="error"/> message.
102+ /// </summary>
103+ /// <param name="error">A message describing the error.</param>
104+ /// <returns>An instance of <see cref="JsonNetResult"/>.</returns>
105+ public static JsonNetResult InternalError ( string error ) {
106+ JsonBody body = new ( HttpStatusCode . InternalServerError , error , null ) ;
107+ return new JsonNetResult ( HttpStatusCode . InternalServerError , body ) ;
108+ }
109+
110+ #endregion
111+
112+ }
113+
114+ }
0 commit comments