Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to not use DeveloperExceptionPageMiddleware

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?

like image 521
Colin DeClue Avatar asked Feb 20 '26 09:02

Colin DeClue


1 Answers

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").

like image 162
Will Huang Avatar answered Feb 22 '26 21:02

Will Huang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!