Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2016-08-06
6
Medium Priority
?
652 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 36

Assisted Solution

by:Miguel Oz
Miguel Oz earned 332 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 332 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 332 total points
ID: 41747307
@funwithdotnet

"Add Service References" only works for SOAP-based services, not RESTful services.
0
Congratulations! You’re Certified – Now What?

Starting a new career can be overwhelming. Becoming certified in your field of expertise is a great start, but where do you go from here?  Here are some tips to help you on your career journey.

 
LVL 10

Accepted Solution

by:
Prakash Samariya earned 1004 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

722 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