Home > Back-end >  Show developer exception page as IActionResult
Show developer exception page as IActionResult

Time:09-18

In Startup.cs I have the following code:

if (env.IsDevelopment())
{
    app.UseDeveloperExceptionPage();
}
else
{
    app.UseExceptionHandler("/Error");
    app.UseHsts();
}

I want my application to still show the developer exception page to admin users during deployment.

In my Error.cshtml.cs, I want to have something like the following:

public IActionResult OnGet()
{
    if (Request.Cookies["admin"] == ReadonlyJsonService.Root.adminCookie)
    {
        // Show developer exception page.   
    }
    else
    {
        // Render Error.cshtml as would happen if I had just put 'public void OnGet() {}'
    }
}

CodePudding user response:

Why not create a custom middleware?

Here we create AppExceptionHandlerMiddleware. It shows exception details if you are in the development environment or you have the admin cookie.

AppExceptionHandlerMiddleware.cs

public class AppExceptionHandlerMiddleware
{
    private readonly RequestDelegate _next;
    private readonly IWebHostEnvironment _env;

    private readonly DeveloperExceptionPageMiddleware developerExceptionPageMiddleware;
    private readonly ExceptionHandlerMiddleware exceptionHandlerMiddleware;

    public AppExceptionHandlerMiddleware(RequestDelegate next, IOptions<DeveloperExceptionPageOptions> dexhOptions,
        IOptions<ExceptionHandlerOptions> exhOptions, ILoggerFactory loggerFactory, IWebHostEnvironment env,
        DiagnosticListener diagnosticListener, DiagnosticSource diagnosticSource, IEnumerable<IDeveloperPageExceptionFilter> filters)
    {
        _next = next;
        _env = env;

        developerExceptionPageMiddleware = new DeveloperExceptionPageMiddleware(next, dexhOptions, loggerFactory, env, diagnosticSource, filters);
        exceptionHandlerMiddleware = new ExceptionHandlerMiddleware(next, loggerFactory, exhOptions, diagnosticListener);
    }

    public async Task Invoke(HttpContext context)
    {
        if (context.Request.Cookies["admin"] == ReadonlyJsonService.Root.adminCookie || _env.IsDevelopment())
        {
            await developerExceptionPageMiddleware.Invoke(context);
        }
        else
        {
            await exceptionHandlerMiddleware.Invoke(context);
        }
    }
}

AppExceptionHandlerExtensions.cs

public static class AppExceptionHandlerExtensions
{
    public static IApplicationBuilder UseAppExceptionHandler(
        this IApplicationBuilder builder)
    {
        return builder.UseMiddleware<AppExceptionHandlerMiddleware>();
    }
}

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    ...
    
    //Config ExceptionHandlerMiddlewareOption via DI.
    services.Configure<ExceptionHandlerOptions>(option =>
    {
        option.ExceptionHandlingPath = new PathString("/Error");
    });

    ...
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseAppExceptionHandler();

    ...
}
  • Related