Skip to content

Commit

Permalink
Make developer exception page work during dev in the sample
Browse files Browse the repository at this point in the history
  • Loading branch information
DamianEdwards committed Dec 17, 2024
1 parent c6f6438 commit 747e70c
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
10 changes: 10 additions & 0 deletions samples/RazorSlices.Samples.WebApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@
app.UseStatusCodePages();
app.UseStaticFiles();

if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
if (Environment.GetEnvironmentVariable("ENABLE_RESPONSE_BUFFERING") == "true")
{
// Enable response buffering middleware to allow for response interception during local development
app.UseResponseBuffering();
}
}

app.MapGet("/lorem", () => Results.Redirect("/lorem-static"));
app.MapGet("/lorem-static", () => Results.Extensions.RazorSlice<Slices.Lorem.LoremStatic>());
app.MapGet("/lorem-dynamic", (int? paraCount, int? paraLength) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"launchBrowser": true,
"applicationUrl": "http://localhost:5270",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
"ASPNETCORE_ENVIRONMENT": "Development",
"ENABLE_RESPONSE_BUFFERING": "true"
}
},
"https": {
Expand All @@ -15,7 +16,8 @@
"launchBrowser": true,
"applicationUrl": "https://localhost:7194;http://localhost:5270",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
"ASPNETCORE_ENVIRONMENT": "Development",
"ENABLE_RESPONSE_BUFFERING": "true"
}
}
}
Expand Down
52 changes: 52 additions & 0 deletions samples/RazorSlices.Samples.WebApp/ResponseBufferingMiddleware.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
namespace RazorSlices.Samples.WebApp;

using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;

internal sealed class ResponseBufferingMiddleware(RequestDelegate next)
{
public async Task InvokeAsync(HttpContext context)
{
// Save the original response body stream
var originalBodyStream = context.Response.Body;

try
{
// Create a memory stream to buffer the response
using var memoryStream = new MemoryStream();
context.Response.Body = memoryStream;

// Call the next middleware
await next(context);

// If no exception occurs, write the buffered response to the original body
memoryStream.Seek(0, SeekOrigin.Begin);
await memoryStream.CopyToAsync(originalBodyStream);
}
catch
{
// Clear the existing response
context.Response.Clear();
context.Response.StatusCode = StatusCodes.Status500InternalServerError;

// Rethrow to let the Developer Exception Page middleware handle it
throw;
}
finally
{
// Restore the original body stream
context.Response.Body = originalBodyStream;
}
}
}

internal static class ResponseBufferingMiddlewareExtensions
{
public static IApplicationBuilder UseResponseBuffering(this IApplicationBuilder app)
{
return app.UseMiddleware<ResponseBufferingMiddleware>();
}
}

0 comments on commit 747e70c

Please sign in to comment.