You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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 ExceptionDetailsProvider to override GetDetails or GetEnvironmentDetails and add custom data (e.g., app-specific exception properties).
  • Build a custom provider: Implement your own IExceptionDetailsProvider from scratch and inject it into your middleware instead of the default.

内容的提问来源于stack exchange,提问作者Rahul Uttarkar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 06:32:09