Files
Joseph Doherty 26ff8d9b4f Initial commit: JDE Scoping Tool migration project
Set up repository with legacy .NET Framework 4.8 source (OLD/),
new .NET 10 Blazor solution (NEW/), OpenSpec specifications,
documentation, and project configuration.
2026-01-02 07:43:29 -05:00

66 lines
2.1 KiB
C#
Executable File

using System;
using System.IO;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
namespace WebInterface.Helpers
{
/// <summary>
/// Custom JSON action result using JSON.NET library
/// </summary>
public class JsonNetResult : JsonResult
{
/// <summary>
/// Serialization settings
/// </summary>
public JsonSerializerSettings Settings { get; }
/// <summary>
/// Constructor
/// </summary>
public JsonNetResult()
{
Settings = new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Error
};
}
/// <summary>
/// Generates the resulting JSON output
/// </summary>
/// <param name="context">Controller context for outputting</param>
public override void ExecuteResult(ControllerContext context)
{
//Verify context is valid before continuing
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
//Verify HTTP method is valid before continuing
if (JsonRequestBehavior == JsonRequestBehavior.DenyGet && string.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
{
throw new InvalidOperationException("JSON GET is not allowed");
}
//Write response
HttpResponseBase response = context.HttpContext.Response;
response.ContentType = string.IsNullOrEmpty(ContentType) ? "application/json" : ContentType;
if (ContentEncoding != null)
response.ContentEncoding = ContentEncoding;
if (Data == null)
return;
var scriptSerializer = JsonSerializer.Create(Settings);
using (var sw = new StringWriter())
{
scriptSerializer.Serialize(sw, Data);
response.Write(sw.ToString());
}
}
}
}