+ Request ID: @RequestId
+
+ Swapping to Development environment will display more detailed information about the error that occurred. +
++ The Development environment shouldn't be enabled for deployed applications. + It can result in displaying sensitive information from exceptions to end users. + For local debugging, enable the Development environment by setting the ASPNETCORE_ENVIRONMENT environment variable to Development + and restarting the app. +
+ +@code{ + [CascadingParameter] public HttpContext? HttpContext { get; set; } + + public string? RequestId { get; set; } + public bool ShowRequestId => !string.IsNullOrEmpty(RequestId); + + protected override void OnInitialized() => + RequestId = Activity.Current?.Id ?? HttpContext?.TraceIdentifier; +} diff --git a/src/Components/test/E2ETest/ServerRenderingTests/ErrorHandlingTest.cs b/src/Components/test/E2ETest/ServerRenderingTests/ErrorHandlingTest.cs new file mode 100644 index 000000000000..219e7acfd4cf --- /dev/null +++ b/src/Components/test/E2ETest/ServerRenderingTests/ErrorHandlingTest.cs @@ -0,0 +1,45 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures; +using Microsoft.AspNetCore.Components.E2ETest.Infrastructure; +using TestServer; +using Components.TestServer.RazorComponents; +using Microsoft.AspNetCore.E2ETesting; +using Xunit.Abstractions; +using OpenQA.Selenium; + +namespace Microsoft.AspNetCore.Components.E2ETests.ServerRenderingTests; + +public class ErrorHandlingTest(BrowserFixture browserFixture, BasicTestAppServerSiteFixture
+ Request ID: @RequestId
+
+ Swapping to Development environment will display more detailed information about the error that occurred. +
++ The Development environment shouldn't be enabled for deployed applications. + It can result in displaying sensitive information from exceptions to end users. + For local debugging, enable the Development environment by setting the ASPNETCORE_ENVIRONMENT environment variable to Development + and restarting the app. +
+ +@code { + [CascadingParameter] + public HttpContext? HttpContext { get; set; } + + private string? RequestId { get; set; } + private bool ShowRequestId => !string.IsNullOrEmpty(RequestId); + + protected override void OnInitialized() => + RequestId = Activity.Current?.Id ?? HttpContext?.TraceIdentifier; +} diff --git a/src/Components/test/testassets/Components.TestServer/RazorComponents/Pages/ErrorLayout.razor b/src/Components/test/testassets/Components.TestServer/RazorComponents/Pages/ErrorLayout.razor new file mode 100644 index 000000000000..a14e57aaee92 --- /dev/null +++ b/src/Components/test/testassets/Components.TestServer/RazorComponents/Pages/ErrorLayout.razor @@ -0,0 +1,7 @@ +@inherits Microsoft.AspNetCore.Components.LayoutComponentBase + +This page throws during OnInitialize to showcase error handling via UseExceptionHandler.
+ +@code +{ + protected override void OnInitialized() => + throw new InvalidOperationException("This page throws on purpose."); +} diff --git a/src/Middleware/Diagnostics/src/ExceptionHandler/ExceptionHandlerExtensions.cs b/src/Middleware/Diagnostics/src/ExceptionHandler/ExceptionHandlerExtensions.cs index 6256709900ba..fafeec524e72 100644 --- a/src/Middleware/Diagnostics/src/ExceptionHandler/ExceptionHandlerExtensions.cs +++ b/src/Middleware/Diagnostics/src/ExceptionHandler/ExceptionHandlerExtensions.cs @@ -47,6 +47,25 @@ public static IApplicationBuilder UseExceptionHandler(this IApplicationBuilder a }); } + ///
+ Request ID: @RequestId
+
+ Swapping to Development environment will display more detailed information about the error that occurred. +
++ The Development environment shouldn't be enabled for deployed applications. + It can result in displaying sensitive information from exceptions to end users. + For local debugging, enable the Development environment by setting the ASPNETCORE_ENVIRONMENT environment variable to Development + and restarting the app. +
+ +@code{ + [CascadingParameter] public HttpContext? HttpContext { get; set; } + + public string? RequestId { get; set; } + public bool ShowRequestId => !string.IsNullOrEmpty(RequestId); + + protected override void OnInitialized() => + RequestId = Activity.Current?.Id ?? HttpContext?.TraceIdentifier; +} diff --git a/src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/BlazorWeb-CSharp/Program.cs b/src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/BlazorWeb-CSharp/Program.cs index 5470d2ce484d..effb8552ca35 100644 --- a/src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/BlazorWeb-CSharp/Program.cs +++ b/src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/BlazorWeb-CSharp/Program.cs @@ -33,7 +33,7 @@ if (!app.Environment.IsDevelopment()) #endif { - app.UseExceptionHandler("/Error"); + app.UseExceptionHandler("/Error", createScopeForErrors: true); #if (HasHttpsProfile) // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); diff --git a/src/ProjectTemplates/test/Templates.Tests/template-baselines.json b/src/ProjectTemplates/test/Templates.Tests/template-baselines.json index 6b982d80f7b2..340a30831f8a 100644 --- a/src/ProjectTemplates/test/Templates.Tests/template-baselines.json +++ b/src/ProjectTemplates/test/Templates.Tests/template-baselines.json @@ -519,6 +519,7 @@ "Components/Layout/MainLayout.razor.css", "Components/Layout/NavMenu.razor", "Components/Layout/NavMenu.razor.css", + "Components/Pages/Error.razor", "Components/Pages/Home.razor", "Components/Pages/Weather.razor", "Properties/launchSettings.json", @@ -545,6 +546,7 @@ "Components/Layout/NavMenu.razor", "Components/Layout/NavMenu.razor.css", "Components/Pages/Counter.razor", + "Components/Pages/Error.razor", "Components/Pages/Home.razor", "Components/Pages/Weather.razor", "Properties/launchSettings.json", @@ -571,6 +573,7 @@ "{ProjectName}/Components/Layout/MainLayout.razor.css", "{ProjectName}/Components/Layout/NavMenu.razor", "{ProjectName}/Components/Layout/NavMenu.razor.css", + "{ProjectName}/Components/Pages/Error.razor", "{ProjectName}/Components/Pages/Home.razor", "{ProjectName}/Components/Pages/Weather.razor", "{ProjectName}/Properties/launchSettings.json", @@ -603,6 +606,7 @@ "{ProjectName}/Components/Layout/MainLayout.razor.css", "{ProjectName}/Components/Layout/NavMenu.razor", "{ProjectName}/Components/Layout/NavMenu.razor.css", + "{ProjectName}/Components/Pages/Error.razor", "{ProjectName}/Components/Pages/Home.razor", "{ProjectName}/Components/Pages/Weather.razor", "{ProjectName}/Properties/launchSettings.json", @@ -631,6 +635,7 @@ "Components/Layout/NavMenu.razor", "Components/Layout/NavMenu.razor.css", "Components/Pages/Counter.razor", + "Components/Pages/Error.razor", "Components/Pages/Home.razor", "Components/Pages/Weather.razor", "Components/Routes.razor", @@ -665,6 +670,7 @@ "{ProjectName}.Client/Layout/NavMenu.razor", "{ProjectName}.Client/Layout/NavMenu.razor.css", "{ProjectName}.Client/Pages/Counter.razor", + "{ProjectName}.Client/Pages/Error.razor", "{ProjectName}.Client/Pages/Home.razor", "{ProjectName}.Client/Pages/Weather.razor", "{ProjectName}.Client/Program.cs", @@ -697,6 +703,7 @@ "{ProjectName}.Client/Layout/NavMenu.razor", "{ProjectName}.Client/Layout/NavMenu.razor.css", "{ProjectName}.Client/Pages/Counter.razor", + "{ProjectName}.Client/Pages/Error.razor", "{ProjectName}.Client/Pages/Home.razor", "{ProjectName}.Client/Pages/Weather.razor", "{ProjectName}.Client/Program.cs", @@ -722,6 +729,7 @@ "Components/_Imports.razor", "Components/Layout/MainLayout.razor", "Components/Layout/MainLayout.razor.css", + "Components/Pages/Error.razor", "Components/Pages/Home.razor", "Properties/launchSettings.json", "wwwroot/app.css" @@ -741,6 +749,7 @@ "Components/_Imports.razor", "Components/Layout/MainLayout.razor", "Components/Layout/MainLayout.razor.css", + "Components/Pages/Error.razor", "Components/Pages/Home.razor", "Properties/launchSettings.json", "wwwroot/app.css" @@ -757,6 +766,7 @@ "{ProjectName}/{ProjectName}.csproj", "{ProjectName}/Program.cs", "{ProjectName}/Components/App.razor", + "{ProjectName}/Components/Pages/Error.razor", "{ProjectName}/Components/Routes.razor", "{ProjectName}/Components/_Imports.razor", "{ProjectName}/Components/Layout/MainLayout.razor", @@ -784,6 +794,7 @@ "{ProjectName}/Components/_Imports.razor", "{ProjectName}/Components/Layout/MainLayout.razor", "{ProjectName}/Components/Layout/MainLayout.razor.css", + "{ProjectName}/Components/Pages/Error.razor", "{ProjectName}/Components/Pages/Home.razor", "{ProjectName}/Properties/launchSettings.json", "{ProjectName}/wwwroot/app.css",