学习笔记

架构班学习


各种内置扩展用法

<h3>1. <code>app.Run</code></h3> <p><code>app.Run</code> 用于定义终端中间件,这意味着一旦执行到 <code>app.Run</code>,请求管道将停止,并返回响应。</p> <pre><code class="language-csharp"> app.Run(async context =&amp;gt; { await context.Response.WriteAsync(&amp;quot;Hello, this is the end of the pipeline!&amp;quot;); });</code></pre> <h3>2. <code>app.UseWhen</code></h3> <p><code>app.UseWhen</code> 根据指定的条件执行中间件分支。如果条件为真,则请求将转到分支中进行处理。</p> <pre><code class="language-csharp">app.UseWhen(context =&amp;gt; context.Request.Query.ContainsKey(&amp;quot;Name&amp;quot;), appBuilder =&amp;gt; { appBuilder.Run(async context =&amp;gt; { await context.Response.WriteAsync(&amp;quot;Query contains 'Name'.&amp;quot;); }); });</code></pre> <h3>3. <code>app.Map</code></h3> <p><code>app.Map</code> 根据请求路径执行特定的中间件。它会创建一个新的请求管道分支,只有在请求路径匹配时才会执行。</p> <pre><code class="language-csharp">app.Map(&amp;quot;/path&amp;quot;, appBuilder =&amp;gt; { appBuilder.Run(async context =&amp;gt; { await context.Response.WriteAsync(&amp;quot;This is the mapped path.&amp;quot;); }); });</code></pre> <h3>4. <code>app.MapWhen</code></h3> <p><code>app.MapWhen</code> 根据指定的条件执行中间件分支,类似于 <code>app.UseWhen</code>,但更灵活,可以根据任意条件进行分支处理。</p> <pre><code class="language-csharp">app.MapWhen(context =&amp;gt; context.Request.Query.ContainsKey(&amp;quot;Name&amp;quot;), appBuilder =&amp;gt; { appBuilder.Run(async context =&amp;gt; { await context.Response.WriteAsync(&amp;quot;Query contains 'Name'.&amp;quot;); }); });</code></pre> <h3>5. <code>app.UseMiddleware</code></h3> <p><code>app.UseMiddleware</code> 用于将自定义中间件添加到请求管道。自定义中间件通常是一个类,包含一个 <code>Invoke</code> 或 <code>InvokeAsync</code> 方法。</p> <pre><code class="language-csharp">public class CustomMiddleware { private readonly RequestDelegate _next; public CustomMiddleware(RequestDelegate next) { _next = next; } public async Task InvokeAsync(HttpContext context) { // Do something before await context.Response.WriteAsync(&amp;quot;Custom Middleware before.\n&amp;quot;); await _next(context); // Do something after await context.Response.WriteAsync(&amp;quot;Custom Middleware after.\n&amp;quot;); } } // 使用中间件 app.UseMiddleware&amp;lt;CustomMiddleware&amp;gt;();</code></pre> <h3>6. <code>app.UseExceptionHandler(&amp;quot;Home/Error&amp;quot;)</code>与<code>app.UseDeveloperExceptionPage()</code></h3> <p><code>app.UseExceptionHandler</code>与<code>app.UseDeveloperExceptionPage</code> 错误页面、全局异常处理</p> <h3>综合示例</h3> <p>以下是一个综合示例,展示如何在 ASP.NET Core 中使用这些方法来构建复杂的请求管道:</p> <pre><code class="language-csharp">var builder = WebApplication.CreateBuilder(args); var app = builder.Build(); // 终端中间件 app.Run(async context =&amp;gt; { await context.Response.WriteAsync(&amp;quot;This is the end of the pipeline.&amp;quot;); }); // 根据查询字符串条件使用分支中间件 app.UseWhen(context =&amp;gt; context.Request.Query.ContainsKey(&amp;quot;Name&amp;quot;), appBuilder =&amp;gt; { appBuilder.Run(async context =&amp;gt; { await context.Response.WriteAsync(&amp;quot;Query contains 'Name'.\n&amp;quot;); }); }); // 映射路径 app.Map(&amp;quot;/path&amp;quot;, appBuilder =&amp;gt; { appBuilder.Run(async context =&amp;gt; { await context.Response.WriteAsync(&amp;quot;This is the mapped path.\n&amp;quot;); }); }); // 根据条件映射 app.MapWhen(context =&amp;gt; context.Request.Query.ContainsKey(&amp;quot;Age&amp;quot;), appBuilder =&amp;gt; { appBuilder.Run(async context =&amp;gt; { await context.Response.WriteAsync(&amp;quot;Query contains 'Age'.\n&amp;quot;); }); }); // 使用自定义中间件 app.UseMiddleware&amp;lt;CustomMiddleware&amp;gt;(); app.Run();</code></pre>

页面列表

ITEM_HTML