WebApi Get Using jQuery.

They are many times when building web applications that we need to return data via a web API in asp.net. I have seen many posts on StackOverflow and ASP.NET with numerous different ways to achieve this, and developers still having problems.

So in this blog, I will show how you can make a web API request to an asp.net web API and return a 200 status with data.

WebApiConfig

To start, ensure that you have the WebApiConfig file added to your project.

Application_Start()

Now ensure that the code below is in your Global.asax file as below.

API Controller

Next for the web API, we need to create the API controller, and the code for this is below.

As you can see, its a straightforward web API controller with a RoutePrefix named 'api/web' and the route called 'returnvalue'.

If you have PostMan installed on your machine, then fire it up and enter the following: https://localhost:YourPortNUmber/api/web/returnvalue replacing YourPortNumber with the port on your computer. If all goes to plan, then it should make a GET request to your web API, and you will see something similar to the image below.

WebApi Get

 

Now that we know it works in PostMan, we can create a simple jQuery file that does an ajax GET request to the web API and the code for that is below.

JQuery

Running all the code and viewing the jQuery in Chrome developer, you will see the data returned via the ajax call as the image below shows.

Jquery

Hopefully, if you followed along, then everything should have worked OK for you, if yes please leave a comment below.

Leave a comment

*
*