How to not use DeveloperExceptionPageMiddleware
Asked Answered
U

2

11

When using the new template for ASP.NET Core 6.0, which includes var builder = WebApplication.CreateBuilder(args); the DeveloperExceptionPageMiddleware is automatically added. I would like to not use this middleware so that I can provide a consistent error response in all environments.

Is there anyway to put my custom error handling before this middleware, or prevent it from being included? Or to remove it after it's been included?

Ulrika answered 15/11, 2021 at 23:14 Comment(0)
S
11

Currently you can't disable DeveloperExceptionPageMiddleware in minimal hosting model for development environment since it is set up without any options to configure. So options are:

  1. Use/switch back to generic host model (the one with Startups) and skip the UseDeveloperExceptionPage call.

  2. Do not use development environment

  3. Setup custom exception handling. DeveloperExceptionPageMiddleware relies on the fact that exception was not handled later down the pipeline so adding custom exception handler right after the building the app should do the trick:

app.Use(async (context, func) =>
{
    try
    {
        await func();
    }
    catch (Exception e)
    {
        context.Response.Clear();

        // Preserve the status code that would have been written by the server automatically when a BadHttpRequestException is thrown.
        if (e is BadHttpRequestException badHttpRequestException)
        {
            context.Response.StatusCode = badHttpRequestException.StatusCode;
        }
        else
        {
            context.Response.StatusCode = 500;
        }
        // log, write custom response and so on...
    }
});

Using custom exception handler page with UseExceptionHandler also should (unless the handler itself throws πŸ™‚) work.

Sleek answered 16/11, 2021 at 7:54 Comment(6)
Unfortunately, neither of the suggestions in #2 work. Custom exception handling with UseExceptionHandler never triggers, and the exception doesn't bubble up to any other middleware as it's caught in DeveloperExceptionPageMiddleware and not passed further. – Ulrika
@ColinDeClue I have tested this code locally and it worked for me (both for UseExceptionHandler (note that you need to create an endpoint/page/action for it)). Can you please share the repro somewhere on the github? Also note that DeveloperExceptionPageMiddleware is executed after all custom registered middlewares. – Sleek
Well, turns out it DOES work just fine... I had a try...catch in the controller I was using for testing... – Ulrika
@PoulBak not following you. the provided code was tested and it works. Also as far as I remember UseExceptionHandler also worked when I tested it. – Sleek
UseExceptionHandler does work (sorry for the confusion, it was indeed caused by my failing ExceptionHandler page). – Schroder
@PoulBak No problem. If I remember correctly I had exactly the same issue with UseExceptionHandler when tried it for the first time) – Sleek
B
11

The easiest way to skip DeveloperExceptionPageMiddleware is not using the Development environment.

You can modify the Properties/launchSettings.json file, change ASPNETCORE_ENVIRONMENT to anything but Development.

{
  "$schema": "https://json.schemastore.org/launchsettings.json",
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:43562",
      "sslPort": 44332
    }
  },
  "profiles": {
    "api1": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "launchUrl": "swagger",
      "applicationUrl": "https://localhost:7109;http://localhost:5111",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "MyDevelopment"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "swagger",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "MyDevelopment"
      }
    }
  }
}

In your app, change all builder.Environment.IsDevelopment() to builder.Environment.IsEnvironment("MyDevelopment").

Bargeman answered 4/12, 2021 at 11:55 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.