如何在ASP.NET代码页面中加载Web API [英] How to cal web API in ASP.NET code behind page

查看:101
本文介绍了如何在ASP.NET代码页面中加载Web API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在页面后面的Asp.Net代码中调用Web API?

Url:

How to cal Web API in Asp.Net Code Behind Page ?
Url:

<a href=""></a><a href="http://111.93.203.157:8063/api/notification/eventnews"></a>[<a href="http://111.93.203.157:8063/api/notification/eventnews" target="_blank" title="New Window">^</a>]





结果返回:

{

数据:{

结果:[

{

NewsEventId:1,

EventName:Bijaya Sammelani,

描述:Happy Bijaya dasami,

EventDate:2017-10-13T00: 00:00\" ,<无线电通信/>
CreatedDate:2017-03-09T18:20:16.41,

ActiveFlag:0,

EventImage:2 .png,

EventImageURL:http://111.93.203.157:8099/EventImage/

},

{

NewsEventId:2,

EventName:年度日,

描述:公司的年度日,

EventDate:2001-04-20T00:00:00,

CreatedDate:2017-03-27T12:52:35.947,

ActiveFlag:0,

EventImage:3. png,

EventImageURL:http://111.93.203.157 :8099 / EventImage /

}

],

错误:null

}

}



这是我的网络API和返回值。

我不明白如何在我的网址中调用此网页API Web应用程序。我在Web窗体中工作所以我需要这个api调用我的代码页面(.cs)页面。我对使用Ajax不感兴趣。只需要如何在页面后面调用此代码并获取此值。



我尝试过:



我不明白如何在我的网络应用程序中调用此web api。我在Web窗体中工作所以我需要这个api调用我的代码页面(.cs)页面。我对使用Ajax不感兴趣。只需要如何在页面后面调用此代码并获取此值。



Result Return:
{
"data": {
"result": [
{
"NewsEventId": 1,
"EventName": "Bijaya Sammelani",
"Description": "Happy Bijaya dasami",
"EventDate": "2017-10-13T00:00:00",
"CreatedDate": "2017-03-09T18:20:16.41",
"ActiveFlag": 0,
"EventImage": "2.png",
"EventImageURL": "http://111.93.203.157:8099/EventImage/"
},
{
"NewsEventId": 2,
"EventName": "Annual Day",
"Description": "Annual day of the Company",
"EventDate": "2001-04-20T00:00:00",
"CreatedDate": "2017-03-27T12:52:35.947",
"ActiveFlag": 0,
"EventImage": "3.png",
"EventImageURL": "http://111.93.203.157:8099/EventImage/"
}
],
"error": null
}
}

This my web api and return value.
I am not understand how to call this web api in my web application. My working in Web Form so i need to this api call my code behind page(.cs) page. I am not interested to use Ajax. Only need how to call this code behind page and Get this value.

What I have tried:

I am not understand how to call this web api in my web application. My working in Web Form so i need to this api call my code behind page(.cs) page. I am not interested to use Ajax. Only need how to call this code behind page and Get this value.

推荐答案



@DiptyajitDey:


@DiptyajitDey:

我不明白如何在我的网络应用程序中调用此web api。我在Web窗体中工作所以我需要这个api调用我的代码页面(.cs)页面。我对使用Ajax不感兴趣。只需要如何在页面后面调用此代码并获取此值。

I am not understand how to call this web api in my web application. My working in Web Form so i need to this api call my code behind page(.cs) page. I am not interested to use Ajax. Only need how to call this code behind page and Get this value.



我在ASP.NET MVC中有一个例子:使用Bootstrap和AngularUI Bootstrap的Asp.net MVC警告横幅 [ ^ ]



但是,这个使用ASP.NET Web Forms的示例,应该让你入门


I have an example in ASP.NET MVC: Asp.net MVC Warning Banner using Bootstrap and AngularUI Bootstrap[^]

But, this example using ASP.NET Web Forms, should get you started

public class Product
{
    public int userId { get; set; }
    public int id { get; set; }
    public string title { get; set; }
    public string body { get; set; }
}

static void GetSomething()
{
    Product product = null;
    using (var client = new System.Net.Http.HttpClient())
    {
        client.BaseAddress = new Uri("http://jsonplaceholder.typicode.com");
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
        var response = client.GetAsync("posts/1").Result;
        if (response.IsSuccessStatusCode)
        {
            string responseString = response.Content.ReadAsStringAsync().Result;
            Newtonsoft.Json.Linq.JObject json = Newtonsoft.Json.Linq.JObject.Parse(responseString);
            product = Newtonsoft.Json.JsonConvert.DeserializeObject<Product>(responseString);
        }
    }
}

protected void Page_Load(object sender, EventArgs e)
{
    GetSomething();
}


您需要使用
HttpClient

来执行此操作。



您还需要

to do this.

You also require

JSON.net library

将JSon字符串转换为指定的dotnet类型。



查看这篇文章为清楚起见。

to convert the JSon string to the specified dotnet type.

Check this article for clarity.


这篇关于如何在ASP.NET代码页面中加载Web API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆