title: .net core web api - Can controller pass parameters to middleware?
body: <p>Hi I need to catch exceptions for http requests, e.g.:</p>
  <pre><code>    [HttpPost(&quot;Test&quot;)]
      public async Task&lt;ActionResult&lt;TestResponse&gt;&gt; Test(TestRequest request)
      {
          TestResponse result;
          try
          {
             // call 3rd party service
          }
          catch(exception ex)
          {
            result.Errorcode = &quot;Mock&quot; // This Errorcode will be used by client side
          }
  
          return Ok(result);
      }
  </code></pre>
  <p>now since there are many http requests, I want to use a middleware to globally handle exceptions rather than<br />
  writing try-catch statement in each http request as above.</p>
  <pre><code>public class Middleware
  {
      readonly RequestDelegate next;
  
      public Middleware(RequestDelegate next)
      {
          this.next = next;
      }
  
      public async Task InvokeAsync(HttpContext httpContext)
      {
          try
          {
              await next(httpContext);
          }
          catch (Exception ex)
          {
              // is there a way to pass TestResponse here so I can do  result.Errorcode = &quot;Mock&quot;?
          }
      }
  }
  </code></pre>
  <p>I do not know how to assign Errorcode using the middleware approach as I comment out above. Is it possible? Thanks.</p>