Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

How to call a Web Api from from a Visual webpart code behind?

$
0
0

Hi,

I am trying to create a visual web part in sharepoint 2013 with data received from another Web API.

I followed the below steps.

1. Created a Visual Web part.

2. In the code behind(.cs) file I wrote the following code.

 async private void GetResult()
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://localhost:8080/");
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                var response = await client.GetAsync("api/Tfs/OpenEnquiriesCount");
                var content = response.Content;
}}

3. When I run the application, I get security exception in the line await client.GetAsync()

What is the way to achieve this? How to call a web api from share point visual web part?

Thank you in advance.


Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>