C# aspnet webapp, I have the following constructor in my middleware:
public MiddlewareCustomRuntimeExceptionHandler (RequestDelegate next, ILogger<MiddlewareCustomRuntimeExceptionHandler> logger, bool showDeveloperDebugInfo, bool sessionEnabled) {
_next = next;
_logger = logger;
_showDeveloperDebugInfo = showDeveloperDebugInfo;
_sessionEnabled = sessionEnabled;
}
This works fine and as expected, true and false values are passed correctly to parameters showDeveloperDebugInfo
and sessionEnabled
:
app.UseMiddleware<MiddlewareCustomRuntimeExceptionHandler>(true, false);
But I would rather pass the parameters showDeveloperDebugInfo
and sessionEnabled
as named parameters.
This does not work:
app.UseMiddleware<MiddlewareCustomRuntimeExceptionHandler>(
showDeveloperDebugInfo: true,
sessionEnabled: true
);
This one reports The best overload for 'UseMiddleware' does not have a parameter named 'next' :
app.UseMiddleware<MiddlewareCustomRuntimeExceptionHandler>(
next: null,
Logger: null,
showDeveloperDebugInfo: true,
sessionEnabled: true
);
What is the correct syntax or is it impossible to use named parameters here?
I had the following setup:
Visual Studio Code: 1.83.1
.NET SDKs installed: 7.0.403
.NET runtimes installed: Microsoft.AspNetCore.App 7.0.13, Microsoft.NETCore.App 7.0.13, Microsoft.WindowsDesktop.App 7.0.13
Windows 11, Version: 10.0.22621
Middleware does not support passing parameters directly.
If you want pass the params, you can try this.
MyMiddleware.cs
Program.cs