Tech Jay
Tech Jay

Reputation: 434

calling webapi from asp.net application as a client

I have a web api with a method to return string value. I am having this web api controller inside a web application. I am trying to call the method in the api using the following code:

        Stream data = client.OpenRead(new Uri("http://localhost:40786/api/Getvalues/getstring"));
        StreamReader reader = new StreamReader(data);
        string s = reader.ReadToEnd();
        Console.WriteLine(s);
        data.Close();
        reader.Close();

I am calling this method on click event of a web-page say Default.aspx. The code runs fine however instead of calling web-api and returning it's value, it is returning HTML mark-up of the page on which i have the button. No idea what is happening. Can anyone suggest what I am missing here ?

Upvotes: 0

Views: 140

Answers (1)

David Abaev
David Abaev

Reputation: 696

Try this code:

WebClient client = new WebClient();
string resultStr = client.DownloadString(http://localhost:40786/api/Getvalues/getstring");

Upvotes: 1

Related Questions