Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 799
  • Last Modified:

how to make a call to Rest Web API from asp.net web application

Hi, How to make a call from Rest Web API (created in VS 2013 using C# .NET 4.5) from Asp. Net web application(created in VS 2013 using .NET 4.5) .The client application is not MVC, just web application. Plz advise.

Thanks
0
KavyaVS
Asked:
KavyaVS
4 Solutions
 
Miguel OzSoftware EngineerCommented:
Please follow the steps in this link.
The example shows a console app but  the call code is valid for any .NET client.
0
 
funwithdotnetCommented:
Right-click on References and add a service reference to the asp.net app.
0
 
käµfm³d 👽Commented:
@funwithdotnet

"Add Service References" only works for SOAP-based services, not RESTful services.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Prakash SamariyaIT ProfessionalCommented:
To call/consume Rest Web API, there are various ways; you can use RestSharp (which is Http api client)

http://social.technet.microsoft.com/wiki/contents/articles/31792.asp-net-how-to-consume-web-api-from-mvc4-using-restsharp.aspx

If you are familiar with HttpClient request, you can make HttpClient request and consume webapi.

Code Logic  like below: [Suppose you have Sample webapi is created]
public class Sample
{
	public int Id {get;set;}
	public string Name {get;set;}
}

//Create Helper to consume Rest WebApi
public class SampleRESTService {

    readonly string uri = "http://localhost:2236/api/Samples";

    public async Task<List<Sample>> GetSamplesAsync() {

        using (HttpClient httpClient = new HttpClient()) {

            return JsonConvert.DeserializeObject<List<Sample>>(
                await httpClient.GetStringAsync(uri)    
            );
        }
    }
}

public class HomeController : Controller {

	//Create object of Helper Class to consume webapi
    private SampleRESTService service = new SampleRESTService();

    public async Task<ActionResult> Index() {

        //Consume webapi and get result
		return View("index", await service.GetSamplesAsync() );
    }
}

Open in new window

0
 
KavyaVSAuthor Commented:
Thanks
0
 
KavyaVSAuthor Commented:
Thanks
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now