As ASP.NET MVC and Web APIs are almost the same, Microsoft combined them both in ASP.NET Core platform and made it more featured and powerful. Earlier, we discussed how to quickly configure MVC in ASP.NET Core from scratch and view injection using inject in ASP.NET Core MVC. Those articles were more focused on ASP.NET Core MVC. In this post, we will create simple web API (REST) and test it from browser.
Let's begin.
In Visual Studio, create a new project (File > New > Project), and select ASP.NET Core Web Application (.NET Core).

Assign a name to the application CoreWebAPI and click OK.
In dialog, select Web API template and click OK. This will generate default ASP.NET Core Web API project.

Let's begin.
In Visual Studio, create a new project (File > New > Project), and select ASP.NET Core Web Application (.NET Core).

Assign a name to the application CoreWebAPI and click OK.
In dialog, select Web API template and click OK. This will generate default ASP.NET Core Web API project.

Let's see what has been changed in project.json file. As ASP.NET Core provides a common platform for MVC and Web API, it will include Microsoft.AspNetCore.Mvc instead of its own web API dependencies.
Once we create a web API project, it will generate default ValuesController which has default Get, Put, Post, and Delete actions. We will ignore it and create our own API Controller.
Once we create a web API project, it will generate default ValuesController which has default Get, Put, Post, and Delete actions. We will ignore it and create our own API Controller.
The setup is over. Let's create new API to expose the data from API call.
Add a Controller class
In Solution Explorer, right click on Controllers folder, Add > New Item. Select web API controller class. Enter the controller name ProductController and tap Add.

Visual studio scaffold's default actions whenever we create a new web API Controller Class.
- [Route("api/[controller]")]
- public class ProductController : Controller
- {
- // GET: api/values
- [HttpGet]
- public IEnumerable<string> Get()
- {
- return new string[] { "value1", "value2" };
- }
- // GET api/values/5
- [HttpGet("{id}")]
- public string Get(int id)
- {
- return "value";
- }
- // POST api/values
- [HttpPost]
- public void Post([FromBody]string value)
- {
- }
- // PUT api/values/5
- [HttpPut("{id}")]
- public void Put(int id, [FromBody]string value)
- {
- }
- // DELETE api/values/5
- [HttpDelete("{id}")]
- public void Delete(int id)
- {
- }
- }
If you have observed, API Controller is inheriting Controller base class instead of ApiController. ASP.NET Core makes it happen.
Add a model class
Add a new folder in the project and name it "Models". Now, right click on the Models folder and Add > Class. Name the class Product and click Add.
Add a model class
Add a new folder in the project and name it "Models". Now, right click on the Models folder and Add > Class. Name the class Product and click Add.


srinu vasPosted Apr 5, 2019, 2:40 AM
Nice Articles
Bhavik PatelPosted Aug 18, 2016, 10:56 AM
Thanks Ramesh K and Vignesh Mani
Vignesh ManiPosted Aug 18, 2016, 8:35 AM
Nice
Ramesh PalaniappanPosted Aug 18, 2016, 5:14 AM
Good One