Skip to content

Commit a28db88

Browse files
committed
Added JsonResult and controller base extensions for IActionResult objects
1 parent 9570f39 commit a28db88

3 files changed

Lines changed: 169 additions & 0 deletions

File tree

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
namespace MADE.Web.Mvc.Extensions
2+
{
3+
using System;
4+
using System.Net;
5+
using MADE.Web.Mvc.Responses;
6+
using Microsoft.AspNetCore.Http;
7+
using Microsoft.AspNetCore.Mvc;
8+
using Microsoft.AspNetCore.Mvc.ModelBinding;
9+
using Newtonsoft.Json;
10+
using JsonResult = MADE.Web.Mvc.Responses.JsonResult;
11+
12+
/// <summary>
13+
/// Defines a collection of extensions for MVC <see cref="ControllerBase"/> instances.
14+
/// </summary>
15+
public static class ControllerBaseExtensions
16+
{
17+
/// <summary>
18+
/// Creates a <see cref="JsonResult"/> object from the specified value for a controller response.
19+
/// </summary>
20+
/// <param name="controller">The controller that is performing the response.</param>
21+
/// <param name="value">The value object to serialize.</param>
22+
/// <param name="statusCode">The expected result HTTP status code.</param>
23+
/// <param name="serializerSettings">The Json.NET serializer settings for serializing the result.</param>
24+
/// <returns>The created <see cref="JsonResult"/> for the response.</returns>
25+
/// <exception cref="ArgumentNullException">Thrown if the <paramref name="controller"/> is <see langword="null"/>.</exception>
26+
public static IActionResult Json(
27+
this ControllerBase controller,
28+
object value,
29+
HttpStatusCode statusCode = HttpStatusCode.OK,
30+
JsonSerializerSettings serializerSettings = null)
31+
{
32+
if (controller == null)
33+
{
34+
throw new ArgumentNullException(nameof(controller));
35+
}
36+
37+
return new JsonResult(value, statusCode, serializerSettings);
38+
}
39+
40+
/// <summary>
41+
/// Creates an <see cref="InternalServerErrorObjectResult"/> that produces a <see cref="StatusCodes.Status500InternalServerError"/> response.
42+
/// </summary>
43+
/// <param name="controller">The controller that is performing the response.</param>
44+
/// <param name="responseContent">An error object to be returned to the client.</param>
45+
/// <returns>The created <see cref="BadRequestObjectResult"/> for the response.</returns>
46+
/// <exception cref="ArgumentNullException">Thrown if the <paramref name="controller"/> is <see langword="null"/>.</exception>
47+
public static IActionResult InternalServerError(this ControllerBase controller, object responseContent)
48+
{
49+
if (controller == null)
50+
{
51+
throw new ArgumentNullException(nameof(controller));
52+
}
53+
54+
return new InternalServerErrorObjectResult(responseContent);
55+
}
56+
57+
/// <summary>
58+
/// Creates an <see cref="InternalServerErrorObjectResult"/> that produces a <see cref="StatusCodes.Status500InternalServerError"/> response.
59+
/// </summary>
60+
/// <param name="controller">The controller that is performing the response.</param>
61+
/// <param name="modelState">The <see cref="ModelStateDictionary" /> containing errors to be returned to the client.</param>
62+
/// <returns>The created <see cref="BadRequestObjectResult"/> for the response.</returns>
63+
/// <exception cref="ArgumentNullException">Thrown if the <paramref name="controller"/> or <paramref name="modelState"/> is <see langword="null"/>.</exception>
64+
public static IActionResult InternalServerError(this ControllerBase controller, ModelStateDictionary modelState)
65+
{
66+
if (controller == null)
67+
{
68+
throw new ArgumentNullException(nameof(controller));
69+
}
70+
71+
if (modelState == null)
72+
{
73+
throw new ArgumentNullException(nameof(modelState));
74+
}
75+
76+
return new InternalServerErrorObjectResult(modelState);
77+
}
78+
}
79+
}

src/MADE.Web.Mvc/MADE.Web.Mvc.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,12 @@
1616
<FrameworkReference Include="Microsoft.AspNetCore.App" />
1717
</ItemGroup>
1818

19+
<ItemGroup>
20+
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
21+
</ItemGroup>
22+
23+
<ItemGroup>
24+
<ProjectReference Include="..\MADE.Web\MADE.Web.csproj" />
25+
</ItemGroup>
26+
1927
</Project>
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
namespace MADE.Web.Mvc.Responses
2+
{
3+
using System;
4+
using System.Net;
5+
using System.Runtime.ExceptionServices;
6+
using System.Threading.Tasks;
7+
using MADE.Web.Extensions;
8+
using Microsoft.AspNetCore.Http;
9+
using Microsoft.AspNetCore.Mvc;
10+
using Microsoft.AspNetCore.Mvc.Infrastructure;
11+
using Newtonsoft.Json;
12+
13+
/// <summary>
14+
/// Defines a model for a result of a request that is serialized as JSON using Json.NET.
15+
/// </summary>
16+
public class JsonResult : ActionResult, IStatusCodeActionResult
17+
{
18+
/// <summary>
19+
/// Initializes a new instance of the <see cref="JsonResult"/> class with the object to serialize.
20+
/// </summary>
21+
/// <param name="value">The value object to serialize.</param>
22+
/// <param name="statusCode">The expected result HTTP status code.</param>
23+
/// <param name="serializerSettings">The Json.Net serializer settings for serializing the result.</param>
24+
public JsonResult(
25+
object value,
26+
HttpStatusCode statusCode = HttpStatusCode.OK,
27+
JsonSerializerSettings serializerSettings = default)
28+
{
29+
this.Value = value;
30+
this.StatusCode = (int)statusCode;
31+
this.SerializerSettings = serializerSettings;
32+
}
33+
34+
/// <summary>
35+
/// Gets the value object to serialize.
36+
/// </summary>
37+
public object Value { get; }
38+
39+
/// <summary>
40+
/// Gets the expected result HTTP status code.
41+
/// </summary>
42+
public int? StatusCode { get; }
43+
44+
/// <summary>
45+
/// Gets the Json.Net serializer settings for serializing the result.
46+
/// </summary>
47+
public JsonSerializerSettings SerializerSettings { get; }
48+
49+
/// <summary>
50+
/// Executes the result operation of the action method asynchronously writing the <see cref="Value"/> to the response.
51+
/// </summary>
52+
/// <param name="context">The context in which the result is executed.</param>
53+
/// <returns>An asynchronous operation.</returns>
54+
/// <exception cref="ArgumentNullException">Thrown if <paramref name="context"/> is <see langword="null"/>.</exception>
55+
public override async Task ExecuteResultAsync(ActionContext context)
56+
{
57+
if (context == null)
58+
{
59+
throw new ArgumentNullException(nameof(context));
60+
}
61+
62+
HttpResponse response = context.HttpContext.Response;
63+
64+
ExceptionDispatchInfo exceptionDispatchInfo = null;
65+
try
66+
{
67+
await response.WriteJsonAsync(
68+
this.StatusCode.GetValueOrDefault((int)HttpStatusCode.OK),
69+
this.Value,
70+
this.SerializerSettings);
71+
}
72+
catch (Exception ex)
73+
{
74+
exceptionDispatchInfo = ExceptionDispatchInfo.Capture(ex);
75+
}
76+
finally
77+
{
78+
exceptionDispatchInfo?.Throw();
79+
}
80+
}
81+
}
82+
}

0 commit comments

Comments
 (0)