title: Calling Web API 2 service from ASP.net WebMethod codebehind
body: <p>I am trying to call my Web API web service from my ASP.net code behind.</p>
  
  <p>Using the <strong>WebMethod</strong> function:</p>
  
  <pre><code>[WebMethod]
  public static string checkQuery(string sql)
  {
      string encryptingIT = new AES().Encrypt(sql);
      string result = q(encryptingIT);
      return result;
  }
  
  public async Task&lt;string&gt; q(string encryptingIT)
  {
      var client = new HttpClient();
      var content = new StringContent(JsonConvert.SerializeObject(new Product { query = encryptingIT, empImg = false }));
      content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
      var response = await client.PostAsync("http://dev-zzzz/newWS/theQ", content);
  
      var value = await response.Content.ReadAsStringAsync();
  
      return value;
  }
  </code></pre>
  
  <p>However, I have an error on line:</p>
  
  <blockquote>
    <p>q(encryptingIT);</p>
  </blockquote>
  
  <p>Which states:</p>
  
  <blockquote>
    <p>Error  16  An object reference is required for the non-static field, method, or property 'WebApi.App._default.q(string)'</p>
  </blockquote>
  
  <p>I tried putting the HttpClient into the same checkQuery function but it seems that I am not allowed to call the function from a asp button click when I do that.</p>
  
  <p>I have used the web service mostly with jQuery Ajax like this:</p>
  
  <pre><code>$.support.cors = true;
  
  $.ajax({
      type: "POST",
      crossDomain: true,
      url: "http://dev-zzzz/newWS/theQ",
      beforeSend: function (xhrObj) {
         xhrObj.setRequestHeader("Content-Type", "application/json");
      },
      data: JSON.stringify({
         theID: "2135648792",
         empImg: "false"
      }),
      dataType: "json",
      success: function (msg) {
  
      },
      error: function (XMLHttpRequest, textStatus, errorThrown) {
  
      }
  });
  </code></pre>
  
  <p>And that works just fine.</p>
  
  <p>What can I do to <strong>mimic that AJAX</strong> in my <strong>code behind WebMethod function</strong>?</p>