1

I wish to call the get function from a button click on my web application

my code in my application is

protected void btngetbtanches_Click(object sender, EventArgs e)
    {
        try
        {


            HttpWebRequest req = WebRequest.Create(@"http://localhost:54691/") as HttpWebRequest;

            WebResponse resp = req.GetResponse();
            using (Stream branchstream = resp.GetResponseStream())
            {
                            StreamReader loResponseStream =
            new StreamReader(branchstream, Encoding.UTF8);

            string Response = loResponseStream.ReadToEnd();

            loResponseStream.Close();
            resp.Close();
            }
            }
        catch (Exception ex)
        {

        }
    }

in my service is

    [ServiceContract]
    public interface IRestSerivce
    {
        [OperationContract]
        [WebGet(UriTemplate = "Default")]
        string GetBranchData();


    }     
}

}

get data is defined in another file in the service project. When I try to click the button some html is returned and the service is not called.

Any help would be appreciated

TraceyB
  • 21
  • 4

1 Answers1

1

Your web request does not match the end point of the service:

You should try:

HttpWebRequest req = WebRequest.Create
        (@"http://localhost:54691/RestSerivce/GetBranchData") as HttpWebRequest;

or at least the one which matches your routing.

You can also add the service as reference and consume it's type, which is a common approache.

You can check this answer for more details. How to consume WCF web service through URL at run time?

Stefan
  • 17,448
  • 11
  • 60
  • 79