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?
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").
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With