title: How to intercept GET request in api before execution in ASP.NET?
body: <p>I'm trying to figure out how to intercept a GET call before execution occurs in .NET framework.</p>
  
  <p>I've created 2 applications: a front-end(calls the API and sends custom HTTP headers with it) and a back-end API:</p>
  
  <p><strong>Front-end method which calls API:</strong></p>
  
  <pre><code>[HttpGet]
      public async Task&lt;ActionResult&gt; getCall()
      {
          string url = "http://localhost:54857/";
          string customerApi = "2";
  
          using (var client = new HttpClient())
          {
              //get logged in userID
              HttpContext context = System.Web.HttpContext.Current;
              string sessionID = context.Session["userID"].ToString();
  
              //Create request and add headers
              client.BaseAddress = new Uri(url);
              client.DefaultRequestHeaders.Accept.Clear();
              client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  
              //Custom header
              client.DefaultRequestHeaders.Add("loggedInUser", sessionID);
  
              //Response
              HttpResponseMessage response = await client.GetAsync(customerApi);
              if (response.IsSuccessStatusCode)
              {
                  string jsondata = await response.Content.ReadAsStringAsync();
                  return Content(jsondata, "application/json");
              }
              return Json(1, JsonRequestBehavior.AllowGet);
          }
      }
  </code></pre>
  
  <p><strong>Back-end which receives the request:</strong></p>
  
  <pre><code>public class RedirectController : ApiController
  {
      //Retrieve entire DB
      ConcurrentDBEntities dbProducts = new ConcurrentDBEntities();
  
      //Get all data by customerID
      [System.Web.Http.AcceptVerbs("GET")]
      [System.Web.Http.HttpGet]
      [System.Web.Http.Route("{id}")]
      public Customer getById(int id = -1)
      {
          //Headers uitlezen
          /*var re = Request;
          var headers = re.Headers;
  
          if (headers.Contains("loggedInUser"))
          {
              string token = headers.GetValues("loggedInUser").First();
          }*/
  
          Customer t = dbProducts.Customers
              .Where(h =&gt; h.customerID == id)
              .FirstOrDefault();
          return t;
      }
  }
  </code></pre>
  
  <p><strong>Routing:</strong></p>
  
  <pre><code>public static void RegisterRoutes(RouteCollection routes)
      {
          routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
  
          routes.MapRoute(
              name: "Default",
              url: "{action}/{id}",
              defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
          );
      }
  </code></pre>
  
  <p>The code shown above works fine, I'm getting the correct results of my API call but I'm looking for a way to intercept <strong>all of the incoming GET request</strong> before im returning a response so i can modify and add logic to this controller. While making my GET request i add custom headers, I'm looking for a way to extract these from the incoming GET all before execution occurs.</p>
  
  <p>Hope someone can help!</p>
  
  <p>Thanks in advance</p>