Events
Power BI DataViz World Championships
Feb 14, 4 PM - Mar 31, 4 PM
With 4 chances to enter, you could win a conference package and make it to the LIVE Grand Finale in Las Vegas
Learn moreThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
This isn't the latest version of this article. For the current release, see the .NET 9 version of this article.
Warning
This version of ASP.NET Core is no longer supported. For more information, see the .NET and .NET Core Support Policy. For the current release, see the .NET 9 version of this article.
Important
This information relates to a pre-release product that may be substantially modified before it's commercially released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
For the current release, see the .NET 9 version of this article.
WebApplication
automatically adds the following middleware in Minimal API applications
depending on certain conditions:
UseDeveloperExceptionPage
is added first when the HostingEnvironment
is "Development"
.UseRouting
is added second if user code didn't already call UseRouting
and if there are endpoints configured, for example app.MapGet
.UseEndpoints
is added at the end of the middleware pipeline if any endpoints are configured.UseAuthentication
is added immediately after UseRouting
if user code didn't already call UseAuthentication
and if IAuthenticationSchemeProvider
can be detected in the service provider. IAuthenticationSchemeProvider
is added by default when using AddAuthentication
, and services are detected using IServiceProviderIsService
.UseAuthorization
is added next if user code didn't already call UseAuthorization
and if IAuthorizationHandlerProvider
can be detected in the service provider. IAuthorizationHandlerProvider
is added by default when using AddAuthorization
, and services are detected using IServiceProviderIsService
.UseRouting
and UseEndpoints
.The following code is effectively what the automatic middleware being added to the app produces:
if (isDevelopment)
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
if (isAuthenticationConfigured)
{
app.UseAuthentication();
}
if (isAuthorizationConfigured)
{
app.UseAuthorization();
}
// user middleware/endpoints
app.CustomMiddleware(...);
app.MapGet("/", () => "hello world");
// end user middleware/endpoints
app.UseEndpoints(e => {});
In some cases, the default middleware configuration isn't correct for the app and requires modification. For example, UseCors should be called before UseAuthentication and UseAuthorization. The app needs to call UseAuthentication
and UseAuthorization
if UseCors
is called:
app.UseCors();
app.UseAuthentication();
app.UseAuthorization();
If middleware should be run before route matching occurs, UseRouting should be called and the middleware should be placed before the call to UseRouting
. UseEndpoints isn't required in this case as it is automatically added as described previously:
app.Use((context, next) =>
{
return next(context);
});
app.UseRouting();
// other middleware and endpoints
When adding a terminal middleware:
UseEndpoints
.UseRouting
and UseEndpoints
so that the terminal middleware can be placed at the correct location.app.UseRouting();
app.MapGet("/", () => "hello world");
app.UseEndpoints(e => {});
app.Run(context =>
{
context.Response.StatusCode = 404;
return Task.CompletedTask;
});
Terminal middleware is middleware that runs if no endpoint handles the request.
WebApplication
automatically adds the following middleware in Minimal API applications
depending on certain conditions:
UseDeveloperExceptionPage
is added first when the HostingEnvironment
is "Development"
.UseRouting
is added second if user code didn't already call UseRouting
and if there are endpoints configured, for example app.MapGet
.UseEndpoints
is added at the end of the middleware pipeline if any endpoints are configured.UseAuthentication
is added immediately after UseRouting
if user code didn't already call UseAuthentication
and if IAuthenticationSchemeProvider
can be detected in the service provider. IAuthenticationSchemeProvider
is added by default when using AddAuthentication
, and services are detected using IServiceProviderIsService
.UseAuthorization
is added next if user code didn't already call UseAuthorization
and if IAuthorizationHandlerProvider
can be detected in the service provider. IAuthorizationHandlerProvider
is added by default when using AddAuthorization
, and services are detected using IServiceProviderIsService
.UseRouting
and UseEndpoints
.The following code is effectively what the automatic middleware being added to the app produces:
if (isDevelopment)
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
if (isAuthenticationConfigured)
{
app.UseAuthentication();
}
if (isAuthorizationConfigured)
{
app.UseAuthorization();
}
// user middleware/endpoints
app.CustomMiddleware(...);
app.MapGet("/", () => "hello world");
// end user middleware/endpoints
app.UseEndpoints(e => {});
In some cases, the default middleware configuration isn't correct for the app and requires modification. For example, UseCors should be called before UseAuthentication and UseAuthorization. The app needs to call UseAuthentication
and UseAuthorization
if UseCors
is called:
app.UseCors();
app.UseAuthentication();
app.UseAuthorization();
If middleware should be run before route matching occurs, UseRouting should be called and the middleware should be placed before the call to UseRouting
. UseEndpoints isn't required in this case as it is automatically added as described previously:
app.Use((context, next) =>
{
return next(context);
});
app.UseRouting();
// other middleware and endpoints
When adding a terminal middleware:
UseEndpoints
.UseRouting
and UseEndpoints
so that the terminal middleware can be placed at the correct location.app.UseRouting();
app.MapGet("/", () => "hello world");
app.UseEndpoints(e => {});
app.Run(context =>
{
context.Response.StatusCode = 404;
return Task.CompletedTask;
});
Terminal middleware is middleware that runs if no endpoint handles the request.
For information on antiforgery middleware in Minimal APIs, see Prevent Cross-Site Request Forgery (XSRF/CSRF) attacks in ASP.NET Core
For more information about middleware see ASP.NET Core Middleware, and the list of built-in middleware that can be added to applications.
For more information about Minimal APIs see Minimal APIs overview
.
ASP.NET Core feedback
ASP.NET Core is an open source project. Select a link to provide feedback:
Events
Power BI DataViz World Championships
Feb 14, 4 PM - Mar 31, 4 PM
With 4 chances to enter, you could win a conference package and make it to the LIVE Grand Finale in Las Vegas
Learn moreTraining
Module
Customize ASP.NET Core behavior with middleware - Training
Understand and implement middleware in an ASP.NET Core app. Use included middleware like HTTP logging and authentication. Create custom middleware to handle requests and responses.