diff --git a/src/Marten.AspNetCore/QueryableExtensions.cs b/src/Marten.AspNetCore/QueryableExtensions.cs index 7ef5d1f9c8..eda35972a0 100644 --- a/src/Marten.AspNetCore/QueryableExtensions.cs +++ b/src/Marten.AspNetCore/QueryableExtensions.cs @@ -276,4 +276,32 @@ public static async Task WriteArray( stream.Position = 0; await stream.CopyToAsync(context.Response.Body, context.RequestAborted).ConfigureAwait(false); } + + /// + /// Write an raw SQL query result directly to the HttpContext + /// + /// + /// + /// + /// + /// Defaults to 200 + /// + public static async Task WriteJson( + this IQuerySession session, + string sql, + HttpContext context, + string contentType = "application/json", + int onFoundStatus = 200 + ) + { + var stream = new MemoryStream(); + _ = await session.StreamJson(stream, context.RequestAborted, sql).ConfigureAwait(false); + + context.Response.StatusCode = onFoundStatus; + context.Response.ContentLength = stream.Length; + context.Response.ContentType = contentType; + + stream.Position = 0; + await stream.CopyToAsync(context.Response.Body, context.RequestAborted).ConfigureAwait(false); + } }