2.ASP.NET Core MVC中间件

按照惯例,通过调用 Use 、MapRun扩展方法,向请求处理管道添加中间件组件。

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddControllersWithViews();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseAuthorization();

app.MapGet("/hi", () => "Hello!");

app.MapDefaultControllerRoute();
app.MapRazorPages();

app.Run();

1.中间件顺序

.ASP.NET

2.Endpoint中间件

.ASP.NET

3.内置中间件

MiddlewareDescriptionOrder
AuthenticationProvides authentication support.Before HttpContext.User is needed. Terminal for OAuth callbacks.
AuthorizationProvides authorization support.Immediately after the Authentication Middleware.
Cookie PolicyTracks consent from users for storing personal information and enforces minimum standards for cookie fields, such as secure and SameSite.Before middleware that issues cookies. Examples: Authentication, Session, MVC (TempData).
CORSConfigures Cross-Origin Resource Sharing.Before components that use CORS. UseCors currently must go before UseResponseCaching due to this bug.
DeveloperExceptionPageGenerates a page with error information that is intended for use only in the Development environment.Before components that generate errors. The project templates automatically register this middleware as the first middleware in the pipeline when the environment is Development.
DiagnosticsSeveral separate middlewares that provide a developer exception page, exception handling, status code pages, and the default web page for new apps.Before components that generate errors. Terminal for exceptions or serving the default web page for new apps.
Forwarded HeadersForwards proxied headers onto the current request.Before components that consume the updated fields. Examples: scheme, host, client IP, method.
Health CheckChecks the health of an ASP.NET Core app and its dependencies, such as checking database availability.Terminal if a request matches a health check endpoint.
Header PropagationPropagates HTTP headers from the incoming request to the outgoing HTTP Client requests.——————————
HTTP LoggingLogs HTTP Requests and Responses.At the beginning of the middleware pipeline.
HTTP Method OverrideAllows an incoming POST request to override the method.Before components that consume the updated method.
HTTPS RedirectionRedirect all HTTP requests to HTTPS.Before components that consume the URL.
HTTP Strict Transport Security (HSTS)Security enhancement middleware that adds a special response header.Before responses are sent and after components that modify requests. Examples: Forwarded Headers, URL Rewriting.
MVCProcesses requests with MVC/Razor Pages.Terminal if a request matches a route.
OWINInterop with OWIN-based apps, servers, and middleware.Terminal if the OWIN Middleware fully processes the request.
Response CachingProvides support for caching responses.Before components that require caching. UseCORS must come before UseResponseCaching.
Response CompressionProvides support for compressing responses.Before components that require compression.
Request LocalizationProvides localization support.Before localization sensitive components. Must appear after Routing Middleware when using RouteDataRequestCultureProvider.
Endpoint RoutingDefines and constrains request routes.Terminal for matching routes.
SPAHandles all requests from this point in the middleware chain by returning the default page for the Single Page Application (SPA)Late in the chain, so that other middleware for serving static files, MVC actions, etc., takes precedence.
SessionProvides support for managing user sessions.Before components that require Session.
Static FilesProvides support for serving static files and directory browsing.Terminal if a request matches a file.
URL RewriteProvides support for rewriting URLs and redirecting requests.Before components that consume the URL.
W3CLoggingGenerates server access logs in the W3C Extended Log File Format.At the beginning of the middleware pipeline.
WebSocketsEnables the WebSockets protocol.Before components that are required to accept WebSocket requests.

原创文章,作者:huoxiaoqiang,如若转载,请注明出处:https://www.huoxiaoqiang.com/csharp/aspnetcoremvc/14604.html

(0)
上一篇 2022年5月1日 01:16
下一篇 2022年5月3日 01:18

相关推荐

  • 5.ASP.NET Core MVC视图

    1.创建视图 右键单击Views文件夹-添加–新建文件夹-名称复数Movies。 右键单击刚刚新建的Views/Movies文件夹-添加–新建项-选择合适的视图选项-名称Index.cshtml–添加。 2.View()帮助方法 2.1视图发现 3.强类型数据 (viewmodel)…

  • 6.ASP.NET Core MVC标记帮助程序

    1.标记帮助程序 1.1@addTagHelper指令 @addTagHelper 指令指定程序集中要加载的标记帮助程序对于 Views 目录或子目录中的所有视图文件均可用。 @addTagHelper 后第一个参数指定程序集中要加载的的标记帮助程序的范围,第二个参数指定包含标记帮助程序的程序集。 …

  • 3.ASP.NET Core MVC终结点路由

    1. UseRouting和UseEndpoints方法 1.1作用 路由使用一对由 UseRouting 和 UseEndpoints 注册的中间件: UseRouting 向中间件管道添加路由匹配。此中间件会查看应用中定义的终结点集,并根据请求选择最佳的终…

发表评论

登录后才能评论