Friday, 5 August 2016

Restful WebApi Getting Started

In this walk through, I would build a webapi for render model data on GET operations.
Open  VS 2015 >New Projects > Visual C#  >  Web> ASP.Net Web Application.
Choose Empty project as shown and enable MVC and WebAPI
In Models folders add class product.cs and define the model as shown
Then in the controllers folder add a new controller and choose Empty WebAPI 2 Controller
Open product controller class and this inherits Api Controller, include reference of model and create object contains product model data. Lets implement HTTP GET verb as shown below, one return complete model and other return specify model based on the model requested for using the Id parameter.
Navigate to WebApiConfig.cs and see the routing configuration.
Lets run the project and you see that webapi isn't available. but its running now navigate to api/product and this renders list of products available and again navigate to the api/product/1 this returns the product model by Id
Using POST-Man, we can test our api as shown below. 

No comments:

Post a Comment

Note: only a member of this blog may post a comment.