Click here to Skip to main content
15,896,207 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to cal Web API in Asp.Net Code Behind Page ?
Url:
HTML
<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>]


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.
Posted
Updated 11-Apr-17 10:07am
v2
Comments
F-ES Sitecore 11-Apr-17 7:16am    
Google "call web api from c#", you can use HttpRequest or webClient to do this depending on your needs\version of .net. That will get you the raw JSON at least and you can turn that JSON into an object by googling "deserialise JSON c#", again it depends what versions of .net you are using etc but there are lots of ways to do it.

https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client
Diptyajit Dey 12-Apr-17 1:43am    
Hi, I saw you exp but problem is i am using post method to collect(get data). I am not using any get method in the web api. I am always using post method for get data. please help me how to using post method to get the Data.


@DiptyajitDey:
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.

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
C#
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();
}
 
Share this answer
 
Comments
Diptyajit Dey 12-Apr-17 1:39am    
Hi Thanks,
i am using post method to get the data. when i am using var response = client.GetAsync("posts/1").Result;
that time if (response.IsSuccessStatusCode) return false. So i am not able to get the value. please help me..
Bryian Tan 12-Apr-17 9:19am    
In the example , post/1 Routes is only allowing Get not Post

JSONPlaceholder - Fake online REST API for developers[^]
You need to use
HttpClient
to do this.

You also require
JSON.net library
to convert the JSon string to the specified dotnet type.

Check this article for clarity.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900