title: ASP.Net Core Api middleware to end request by calling a controller action
body: <p>Server : ASP.net Core Web API</p>
  <p>Client : WinForms app using Refit and sending its version with each request (as a header)</p>
  <p>How does the server check the client requests version, if it is wrong then reply by calling a controller action VersionError?</p>
  <p>This code does not end the request:</p>
  <pre><code>public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  {
      app.UseRouting();
      app.UseAuthorization();
  
      app.Use(async (context, next) =&gt;
      {
          foreach (var header in context.Request.Headers)
          {
              Console.WriteLine($&quot;{context.Request.Path} : {header.Key}={header.Value}&quot;);
  
              if (header.Key == &quot;CLIENT-VERSION&quot; &amp;&amp; header.Value != &quot;5&quot;)
              {
                  context.Request.Method = &quot;GET&quot;;
                  context.Request.Path = &quot;/api/Error/VersionError&quot;;
              }
          }
  
          await next();
      });
  
      app.UseEndpoints(endpoints =&gt;
      {
          endpoints.MapControllers();
      });
  }
  
  [ApiController]
  [Route(&quot;api/[controller]/[action]&quot;)]
  public class ErrorController : ControllerBase
  {
      [HttpGet]
      public Message&lt;string&gt; VersionError()
      {
          var reply = new Message&lt;string&gt;(errorMessage: &quot;version error&quot;);
          return reply;
      }
  }
  </code></pre>