(C# ASP.NET Core) Minimal GET WebApi with No Parameters

In this tutorial we shall add a web api that allows us to get all the records of a table. Please do note, however, that such a query is never done in a real project - it is invariably qualified with some paramaters that return a lesser number of records.
(Rev. 18-Jun-2024)

Categories | About |     |  

Parveen,

The URL for this GET Api

As we told in the first tutorial, a web api is just a function on the server side. This function is executed through a URL. Let us decide for this tutorial that the URL would be a readable url like http://localhost/doctor/all. This url would have been equally good - http://localhost/all, or say, http://localhost/api/doctor/all.

Video Explanation (see it happen!)

Please watch the following youtube video:

Mapping a URL to a WebApi Function

The next step is to connect this url to a function on the server side. This is also called mapping a request to a function.

The easiest way to map a GET request is to use the MapGet function of the WebApplication class.

This function takes 2 parameters - the first is the relative path of the URL and the second is a lambda or request delegate that contains the body of the WebApi function.

In the above case the mapping could be like -


app.MapGet("/doctor/all", () => { . . . });

Let us do it practically!

Open the solution explorer of the starting project that we created in the previous tutorial. Locate the program.cs. Double click and open this file.

Recall that we have added a dbcontext for an in-memory sqlite database. And, after that added some test data to the Doctors table.

And, now just after that, we have added a MapGet request.

// Program.cs file 
// Install-Package Microsoft.AspNetCore.Identity.EntityFrameworkCore 
// Install-Package Microsoft.EntityFrameworkCore.Sqlite 
using Microsoft.EntityFrameworkCore;

using MyRazorApp.Models;

var builder = WebApplication.CreateBuilder();

// the database is created in memory 
builder.Services.AddDbContext<MyApiContext>(
  opt => opt.UseSqlite("DataSource=file:mem?mode=memory&cache=shared")
);

builder.Services.AddRazorPages();

var app = builder.Build();

// seed some test data 
using (var scope = app.Services.CreateScope())

{

  var services = scope.ServiceProvider;

  var ctx = services.GetRequiredService<MyApiContext>();

  ctx.Database.EnsureCreated();

  ctx.Doctors.Add(new Doctor() { Fees = 100, Name = "doc 1" });

  ctx.Doctors.Add(new Doctor() { Fees = 200, Name = "doc 2" });

  ctx.SaveChanges();

}

app.MapGet("/doctor/all", async (MyApiContext ctx) =>
{

  return await ctx.Doctors.ToListAsync();

}

);

app.MapRazorPages();

app.Run();

This request maps the url to a lambda that takes our dbcontext as a parameter. return statement is used to return a list of records. Most of the magic is handled by the ASPNET Core Engine. First it makes the dbcontext available as a parameter, and second it formats the result as a json string and sends the http response as a json string.

Calling the WebApi

Let us now call the webapi. Since it is a GET type of request, we can open the browser and open the URL - /doctor/all. We verify that we indeed get a list of all the records in the table.

  [
    {"id":1,"name":"doc 1","fees":100},

    {"id":2,"name":"doc 2","fees":200}
  ]
  

In the next tutorial we shall learn how to pass parameters to a web api function. Thanks!


This Blog Post/Article "(C# ASP.NET Core) Minimal GET WebApi with No Parameters" by Parveen is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.