Solved

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

Posted on 2016-08-06
6
211 Views
Last Modified: 2016-08-21
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
Comment
Question by:KavyaVS
6 Comments
 
LVL 36

Assisted Solution

by:Miguel Oz
Miguel Oz earned 83 total points
ID: 41745862
Please follow the steps in this link.
The example shows a console app but  the call code is valid for any .NET client.
0
 
LVL 12

Assisted Solution

by:funwithdotnet
funwithdotnet earned 83 total points
ID: 41746543
Right-click on References and add a service reference to the asp.net app.
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 83 total points
ID: 41747307
@funwithdotnet

"Add Service References" only works for SOAP-based services, not RESTful services.
0
Independent Software Vendors: 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!

 
LVL 10

Accepted Solution

by:
Prakash Samariya earned 251 total points
ID: 41748281
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
 

Author Comment

by:KavyaVS
ID: 41764721
Thanks
0
 

Author Closing Comment

by:KavyaVS
ID: 41764727
Thanks
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question