Solved

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

Posted on 2016-08-06
6
93 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 35

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 74

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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 8

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 to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now