Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have web api mvc service that host on IIS. How can consume my service in my new mvc project using ajax

What I have tried:

i trying to add hosted services on my mvc project through add web "add service refrence" and put my url "http://localhost:8080/api/casestatusall" but i found error like "unable to open this Internet site. the requested site is either unavailable or can not be found. Please try again later."
Posted
Updated 7-Jun-17 21:21pm
Comments
F-ES Sitecore 8-Jun-17 4:30am    
Try and put the url in a browser and see what happens. If you can't reach it then the api site isn't set up properly, and as we don't know how you've set it up it's impossible to give much advice.

1 solution

Just the way you will consume it in any other application; console, WPF etc. All that you would need is an HttpClient object and the rest is all documented. :-) Like you have mentioned, you wanted to open it up at "localhost:8080", are you sure that is the location where your API is currently hosted at? Typically, there is a different port unless you want to override everything (which, you should never do!).
Quote:
i trying to add hosted services on my mvc project through add web "add service refrence"
You are confusing Web API with WCF; Web API does not need that much of stupid stuff, to adding service references etc, all you need is an HttpClient and you're done. Rest of the stuff is taken care of by the HTTP protocol and you can consume the API easily, for example the following would work,
C#
var client = new HttpClient();

client.BaseAddress = new Uri("http://localhost:8080/api");

// get something
var something = client.GetAsync("casestatusall");

// Format the response to get the object etc
// You're done.

Have a look at my articles as well, they will guide you on this topic as well.
A Tip for Ajax Developers in ASP.NET MVC Framework[^]
ASP.NET 5 Web API RESTful CRUDs and Windows 10 native application[^]

For more as well, you can look for a proper documentation on HttpClient object and how the HTTP protocol works.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900