title: get controller's type information in middleware (or how to distinct request to web api and for a page)
body: <p>I want to create middleware that handles exceptions but distinct requests to api controller and to razor pages / mvc. In middleware I have only a context.Request.Path url and want to "find" a controller (or page) who is assigned to process the request, then analyze the type information of it and make a decision.</p>
  
  <p>Something like this:</p>
  
  <pre><code>MyExceptionHandlerMiddleware{
     public async Task Invoke(HttpContext context)
     {
         try
         {
            await _next(context);
         }
         catch (Exception ex)
         {
             var path = context.Request.Path;
             var isApiCall = ... ???? 
             if ( isApiCall   )
             {
                var json = GetJson(ex);
                wait context.Response.WriteAsync(json); // return json
             } 
             else {
                await _options.ExceptionHandler(context); // return the page
             }
         }
     }
  }
  </code></pre>
  
  <p>The problem is that it seems regular routing information  unavailable at this point. How to get the contoller's type info? </p>
  
  <p>How to distinct calls to web api and for a page in context of middleware?</p>