AspNetCore自定义DeveloperExceptionPageMiddleware:GetDetails实现查询
Understanding GetDetails(ex) Implementation
Got it, let's break down the exact implementation you're looking for from the ASP.NET Core Diagnostics repo commit you referenced.
First, the _exceptionDetailsProvider in the DisplayRuntimeException method is an instance of ExceptionDetailsProvider—the default implementation of IExceptionDetailsProvider in the Microsoft.AspNetCore.Diagnostics namespace. Here's the full breakdown of how GetDetails(ex) works, including supporting code and model types:
1. Core GetDetails Method
This method recursively processes exceptions (including inner exceptions and aggregate exceptions) to build a structured details object:
public ExceptionDetails GetDetails(Exception exception) { if (exception == null) { throw new ArgumentNullException(nameof(exception)); } var details = new ExceptionDetails { Exception = exception, Message = exception.Message, StackTrace = exception.StackTrace, Source = exception.Source, HResult = exception.HResult, InnerException = exception.InnerException != null ? GetDetails(exception.InnerException) : null, ExceptionType = exception.GetType().FullName, }; // Handle aggregate exceptions by flattening inner exceptions if (exception is AggregateException aggregateException) { details.AggregateExceptionDetails = aggregateException.InnerExceptions.Select(GetDetails).ToList(); } // Attach environment context details details.EnvironmentDetails = GetEnvironmentDetails(); return details; }
2. Supporting GetEnvironmentDetails Helper
This method collects runtime and hosting environment metadata to include in the error details:
private EnvironmentDetails GetEnvironmentDetails() { var hostingEnv = _hostingEnvironmentAccessor?.HostingEnvironment; return new EnvironmentDetails { ApplicationName = hostingEnv?.ApplicationName, EnvironmentName = hostingEnv?.EnvironmentName, AspNetCoreVersion = typeof(Startup).Assembly.GetName().Version?.ToString(), RuntimeVersion = System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription }; }
3. Model Classes: ExceptionDetails & EnvironmentDetails
These are simple DTOs that structure the exception data for rendering in the developer page:
public class ExceptionDetails { public Exception Exception { get; set; } public string Message { get; set; } public string StackTrace { get; set; } public string Source { get; set; } public int HResult { get; set; } public ExceptionDetails InnerException { get; set; } public string ExceptionType { get; set; } public List<ExceptionDetails> AggregateExceptionDetails { get; set; } public EnvironmentDetails EnvironmentDetails { get; set; } } public class EnvironmentDetails { public string ApplicationName { get; set; } public string EnvironmentName { get; set; } public string AspNetCoreVersion { get; set; } public string RuntimeVersion { get; set; } }
How It Fits Into DisplayRuntimeException
In the private DisplayRuntimeException method, the ErrorDetails object returned by GetDetails(ex) is passed to the developer exception page view. This structured data powers all the detailed sections you see: the main exception message, formatted stack trace, nested inner exceptions, aggregate exception breakdown, and environment info like runtime version.
For Your Custom Implementation
If you're building a custom middleware, you have two options to extend this logic:
- Extend the default provider: Create a subclass of
ExceptionDetailsProviderto overrideGetDetailsorGetEnvironmentDetailsand add custom data (e.g., app-specific exception properties). - Build a custom provider: Implement your own
IExceptionDetailsProviderfrom scratch and inject it into your middleware instead of the default.
内容的提问来源于stack exchange,提问作者Rahul Uttarkar

