Skip to content

Commit cc20ff0

Browse files
committed
Supress interactive rendering on error scenarios
1 parent 5dc0694 commit cc20ff0

File tree

6 files changed

+48
-4
lines changed

6 files changed

+48
-4
lines changed

src/Components/Endpoints/src/Microsoft.AspNetCore.Components.Endpoints.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
<Reference Include="Microsoft.AspNetCore.Antiforgery" />
4949
<Reference Include="Microsoft.AspNetCore.Components.Authorization" />
5050
<Reference Include="Microsoft.AspNetCore.Components.Web" />
51+
<Reference Include="Microsoft.AspNetCore.Diagnostics.Abstractions" />
5152
<Reference Include="Microsoft.AspNetCore.DataProtection.Extensions" />
5253
<Reference Include="Microsoft.AspNetCore.Hosting.Abstractions" />
5354
<Reference Include="Microsoft.AspNetCore.Http" />

src/Components/Endpoints/src/Rendering/EndpointHtmlRenderer.Streaming.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ internal partial class EndpointHtmlRenderer
2020
private TextWriter? _streamingUpdatesWriter;
2121
private HashSet<int>? _visitedComponentIdsInCurrentStreamingBatch;
2222
private string? _ssrFramingCommentMarkup;
23+
private bool _allowBoundaryMarkers;
2324

2425
public void InitializeStreamingRenderingFraming(HttpContext httpContext)
2526
{
@@ -189,8 +190,8 @@ protected override void WriteComponentHtml(int componentId, TextWriter output)
189190
protected override void RenderChildComponent(TextWriter output, ref RenderTreeFrame componentFrame)
190191
{
191192
var componentId = componentFrame.ComponentId;
192-
var sequenceAndKey = new SequenceAndKey(componentFrame.Sequence, componentFrame.ComponentKey);
193-
WriteComponentHtml(componentId, output, allowBoundaryMarkers: true, sequenceAndKey);
193+
var sequenceAndKey = new SequenceAndKey(componentFrame.Sequence, componentFrame.ComponentKey);
194+
WriteComponentHtml(componentId, output, _allowBoundaryMarkers, sequenceAndKey);
194195
}
195196

196197
private void WriteComponentHtml(int componentId, TextWriter output, bool allowBoundaryMarkers, SequenceAndKey sequenceAndKey = default)

src/Components/Endpoints/src/Rendering/EndpointHtmlRenderer.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using Microsoft.AspNetCore.Components.Rendering;
1313
using Microsoft.AspNetCore.Components.RenderTree;
1414
using Microsoft.AspNetCore.Components.Routing;
15+
using Microsoft.AspNetCore.Diagnostics;
1516
using Microsoft.AspNetCore.Http;
1617
using Microsoft.AspNetCore.Http.Extensions;
1718
using Microsoft.AspNetCore.Routing;
@@ -59,6 +60,7 @@ private void SetHttpContext(HttpContext httpContext)
5960
if (_httpContext is null)
6061
{
6162
_httpContext = httpContext;
63+
_allowBoundaryMarkers = _httpContext.Features.Get<IExceptionHandlerFeature>() is null;
6264
}
6365
else if (_httpContext != httpContext)
6466
{
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
@page "/Error"
2+
@using System.Diagnostics
3+
4+
<PageTitle>Error</PageTitle>
5+
6+
<h1 class="text-danger">Error.</h1>
7+
<h2 class="text-danger">An error occurred while processing your request.</h2>
8+
9+
@if (ShowRequestId)
10+
{
11+
<p>
12+
<strong>Request ID:</strong> <code>@RequestId</code>
13+
</p>
14+
}
15+
16+
<h3>Development Mode</h3>
17+
<p>
18+
Swapping to <strong>Development</strong> environment will display more detailed information about the error that occurred.
19+
</p>
20+
<p>
21+
<strong>The Development environment shouldn't be enabled for deployed applications.</strong>
22+
It can result in displaying sensitive information from exceptions to end users.
23+
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>
24+
and restarting the app.
25+
</p>
26+
27+
@code{
28+
[CascadingParameter] public HttpContext? HttpContext { get; set; }
29+
30+
public string? RequestId { get; set; }
31+
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
32+
33+
protected override void OnInitialized() =>
34+
RequestId = Activity.Current?.Id ?? HttpContext?.TraceIdentifier;
35+
}

src/Components/Samples/BlazorUnitedApp/Pages/Index.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
[SupplyParameterFromForm] Customer? Value { get; set; }
3535

36-
protected override void OnInitialized() => Value ??= new();
36+
protected override void OnInitialized() => throw new InvalidOperationException("Some error");
3737

3838
bool _submitted = false;
3939
public void Submit() => _submitted = true;

src/Components/Samples/BlazorUnitedApp/Program.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@
33

44
using BlazorUnitedApp;
55
using BlazorUnitedApp.Data;
6+
using Microsoft.AspNetCore.Hosting.StaticWebAssets;
67

78
var builder = WebApplication.CreateBuilder(args);
9+
// Enable this so that we can resolve static web assets in the Production environment
10+
// without publishing the app.
11+
// This is so that we can test the error page.
12+
StaticWebAssetsLoader.UseStaticWebAssets(builder.Environment, builder.Configuration);
813

914
// Add services to the container.
1015
builder.Services.AddRazorComponents();
@@ -16,7 +21,7 @@
1621
// Configure the HTTP request pipeline.
1722
if (!app.Environment.IsDevelopment())
1823
{
19-
app.UseExceptionHandler("/Error");
24+
app.UseExceptionHandler("/Error", newScope: true);
2025
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
2126
app.UseHsts();
2227
}

0 commit comments

Comments
 (0)