简体   繁体   English

从ASP.net WebMethod代码背后调用Web API 2服务

[英]Calling Web API 2 service from ASP.net WebMethod codebehind

I am trying to call my Web API web service from my ASP.net code behind. 我试图从后面的ASP.net代码调用Web API Web服务。

Using the WebMethod function: 使用WebMethod函数:

[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); q(encryptingIT);

Which states: 哪个状态:

Error 16 An object reference is required for the non-static field, method, or property 'WebApi.App._default.q(string)' 错误16非静态字段,方法或属性'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. 我尝试将HttpClient放入相同的checkQuery函数中,但这样做时似乎不允许我从asp按钮单击中调用该函数。

I have used the web service mostly with jQuery Ajax like this: 我将Web服务主要用于jQuery Ajax,如下所示:

$.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 ? 如何在WebMethod函数背后的代码中 模仿AJAX

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;
}

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. 如果要混合和匹配静态成员,请不要将类设为静态。

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 谢谢@ Soham Dasgupta

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM