0

I am trying to call my Web API web service from my ASP.net code behind.

Using the WebMethod function:

[WebMethod]
public static string checkQuery(string sql)
{
    string encryptingIT = new AES().Encrypt(sql);
    string result = q(encryptingIT);
    return result;
}

public async Task<string> 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;
}

However, I have an error on line:

q(encryptingIT);

Which states:

Error 16 An object reference is required for the non-static field, method, or property 'WebApi.App._default.q(string)'

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.

I have used the web service mostly with jQuery Ajax like this:

$.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) {

    }
});

And that works just fine.

What can I do to mimic that AJAX in my code behind WebMethod function?

StealthRT
  • 10,108
  • 40
  • 183
  • 342
  • The error is pretty clear, you can't call an instance function from an static one without a reference to an instance of the class, just set the second one to be static also and you're good, if you must keep that function to be instance then move the code to an static function and call it from the instance one – Gusman Feb 19 '16 at 18:54

3 Answers3

0

Your query method should be static.

[WebMethod]
public static string checkQuery(string sql)
{
    string encryptingIT = new AES().Encrypt(sql);
    string result = q(encryptingIT).Result;
    return result;
}

public static async Task<string> 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;
}
Soham Dasgupta
  • 5,061
  • 24
  • 79
  • 125
  • When i do that I get the error on the same line **Error 17 Cannot implicitly convert type 'System.Threading.Tasks.Task' to 'string'** – StealthRT Feb 19 '16 at 19:20
  • **Error 16 The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'.** And like i said, doing so makes the asp button not fire off the webMethod. – StealthRT Feb 19 '16 at 19:24
  • Oops sorry. You should get the `Task.Result` – Soham Dasgupta Feb 19 '16 at 19:25
  • We generally in `WebMethod`s don't use any async code for the fact that it has to return a value at the end. I would suggest getting rid of the async stuff here as it really doesn't make sense because you're `WebMethod` requests will not be processed asynchronously. You can use [RestSharp](http://restsharp.org/). – Soham Dasgupta Feb 19 '16 at 19:28
  • Got an example, @Soham? – StealthRT Feb 19 '16 at 19:33
  • Thanks for the link but I do not see where its storing the response from the POST? – StealthRT Feb 19 '16 at 19:38
0

A static class cannot be instantiated. Therefore, non-static members could never be accessed.

If you want to mix and match static members, don't make the class static.

MMM
  • 3,132
  • 3
  • 20
  • 32
0

Endded up using this:

[WebMethod]
public static string checkQuery(string sql)
{
    string encryptingIT = new AES().Encrypt(sql);

    var client = new RestClient("http://dev-zzzz/newWS");
    var request = new RestRequest("theQ/", Method.POST);

    request.RequestFormat = DataFormat.Json;

    request.AddBody(new Product
    {
        query = encryptingIT,
        empImg = false
    });

    IRestResponse response = client.Execute(request);
    var content = response.Content;

    return content;
}

Thanks, @Soham Dasgupta

StealthRT
  • 10,108
  • 40
  • 183
  • 342