各种内置扩展用法
<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 =&gt;
{
await context.Response.WriteAsync(&quot;Hello, this is the end of the pipeline!&quot;);
});</code></pre>
<h3>2. <code>app.UseWhen</code></h3>
<p><code>app.UseWhen</code> 根据指定的条件执行中间件分支。如果条件为真,则请求将转到分支中进行处理。</p>
<pre><code class="language-csharp">app.UseWhen(context =&gt; context.Request.Query.ContainsKey(&quot;Name&quot;), appBuilder =&gt;
{
appBuilder.Run(async context =&gt;
{
await context.Response.WriteAsync(&quot;Query contains 'Name'.&quot;);
});
});</code></pre>
<h3>3. <code>app.Map</code></h3>
<p><code>app.Map</code> 根据请求路径执行特定的中间件。它会创建一个新的请求管道分支,只有在请求路径匹配时才会执行。</p>
<pre><code class="language-csharp">app.Map(&quot;/path&quot;, appBuilder =&gt;
{
appBuilder.Run(async context =&gt;
{
await context.Response.WriteAsync(&quot;This is the mapped path.&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 =&gt; context.Request.Query.ContainsKey(&quot;Name&quot;), appBuilder =&gt;
{
appBuilder.Run(async context =&gt;
{
await context.Response.WriteAsync(&quot;Query contains 'Name'.&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(&quot;Custom Middleware before.\n&quot;);
await _next(context);
// Do something after
await context.Response.WriteAsync(&quot;Custom Middleware after.\n&quot;);
}
}
// 使用中间件
app.UseMiddleware&lt;CustomMiddleware&gt;();</code></pre>
<h3>6. <code>app.UseExceptionHandler(&quot;Home/Error&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 =&gt;
{
await context.Response.WriteAsync(&quot;This is the end of the pipeline.&quot;);
});
// 根据查询字符串条件使用分支中间件
app.UseWhen(context =&gt; context.Request.Query.ContainsKey(&quot;Name&quot;), appBuilder =&gt;
{
appBuilder.Run(async context =&gt;
{
await context.Response.WriteAsync(&quot;Query contains 'Name'.\n&quot;);
});
});
// 映射路径
app.Map(&quot;/path&quot;, appBuilder =&gt;
{
appBuilder.Run(async context =&gt;
{
await context.Response.WriteAsync(&quot;This is the mapped path.\n&quot;);
});
});
// 根据条件映射
app.MapWhen(context =&gt; context.Request.Query.ContainsKey(&quot;Age&quot;), appBuilder =&gt;
{
appBuilder.Run(async context =&gt;
{
await context.Response.WriteAsync(&quot;Query contains 'Age'.\n&quot;);
});
});
// 使用自定义中间件
app.UseMiddleware&lt;CustomMiddleware&gt;();
app.Run();</code></pre>