我如何直接从代码隐藏调用ASP.NET Web API?还是应该调用我的
javascript函数,从代码隐藏中调用get
JSON方法?

我通常有如下的东西:

function createFile() {
        $.getJSON("api/file/createfile",function (data) { 
            $("#Result").append('Success!');
        });
    }

任何指针赞赏. TIA.

*我正在使用WebForms.

解决方法

如果您必须调用Web服务本身,您可以尝试使用HttpClient
as described by Henrik Neilsen.

Updated HTTPClient Samples

一个基本的例子:

// Create an HttpClient instance 
HttpClient client = new HttpClient(); 

// Send a request asynchronously continue when complete 
client.GetAsync(_address).ContinueWith( 
    (requestTask) => 
    { 
        // Get HTTP response from completed task. 
        HttpResponseMessage response = requestTask.Result; 

       // Check that response was successful or throw exception 
        response.EnsureSuccessStatusCode(); 

        // Read response asynchronously as JsonValue
        response.Content.ReadAsAsync<JsonArray>().ContinueWith( 
                    (readTask) => 
                    { 
                        var result = readTask.Result
                        //Do something with the result                   
                    }); 
    });

dawei

【声明】:唐山站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。