Certainly! Let’s walk through an example step by step.
Step 1: Create a new ASP.NET Core project
- Open Visual Studio and create a new project.
- Select “ASP.NET Core Web Application” as the project template.
- Choose a name for your project and click “Create.”
- In the next window, select the “API” template and make sure that “ASP.NET Core 3.1” or a newer version is selected.
- Click “Create” to create the project.
Visual Studio will generate a basic project structure for you.
Step 2: Define the API Controller
- In the Solution Explorer, right-click on the “Controllers” folder.
- Select “Add” -> “Controller.”
- In the “Add Scaffold” dialog, choose the “API Controller with actions, using Entity Framework” template.
- Click “Add” to create the controller.
Visual Studio will generate a sample controller for you with some pre-defined actions.
Step 3: Define API endpoints
- Open the created API controller file (e.g.,
ValuesController.cs
). - You will see some sample actions like
Get
,Post
,Put
,Delete
. - Let’s modify the sample actions to return some data.
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
namespace YourProject.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
private static List<string> values = new List<string>()
{
"value1", "value2", "value3"
};
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
return values;
}
[HttpGet("{id}")]
public ActionResult<string> Get(int id)
{
if (id >= 0 && id < values.Count)
return values[id];
return NotFound();
}
[HttpPost]
public ActionResult Post([FromBody] string value)
{
values.Add(value);
return Ok();
}
[HttpPut("{id}")]
public ActionResult Put(int id, [FromBody] string value)
{
if (id >= 0 && id < values.Count)
{
values[id] = value;
return Ok();
}
return NotFound();
}
[HttpDelete("{id}")]
public ActionResult Delete(int id)
{
if (id >= 0 && id < values.Count)
{
values.RemoveAt(id);
return Ok();
}
return NotFound();
}
}
}
In the above example, we have a simple API controller named ValuesController
. It has various actions decorated with HTTP verb attributes (HttpGet
, HttpPost
, HttpPut
, HttpDelete
). The [Route]
attribute is used to specify the route template for the controller and actions.
Step 4: Configure routing
- Open the
Startup.cs
file in your project. - In the
ConfigureServices
method, add the following code to enable API controllers and their features:
services.AddControllers();
- In the
Configure
method, add the following code to configure the routing for your API:
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
Step 5: Test your API
- Build and run your project (press F5 or click the “Start” button in Visual Studio).
- Once the project is running, your API should be accessible via the defined endpoints.
- Open a web browser or use tools like Postman to test the API endpoints:
- GET request to retrieve all values:
https://localhost:port/api/values
- GET request to retrieve a specific value: `https://localhost: port/api/values/{id}`
- POST request to add a new value:
https://localhost:port/api/values
with the value in the request body - PUT request to update a value:
https://localhost:port/api/values/{id}
with the updated value in the request body - DELETE request to remove a value:
https://localhost:port/api/values/{id}
- GET request to retrieve all values:
By following these steps, you have created a simple Web API in ASP.NET Core. You can further expand and customize the API by adding more actions, implementing authentication and authorization, connecting to a database, and so on.