Web API with ASP.NET and SQLITE — PART 3

Rashmi Milan
Nov 6 · 3 min read

In last two posts [Part1, Part 2], we learnt how to create a ASP.Net Web Application Project and use the EntitiyFrameWorkCore to connect to SQLite Database as well as create the database.

In the concluding part, we will learn how to use a controller which will help the API to query data and return to it to the user.

STEP 1: Add Controller

Whenever we run the project, by default the browser webpage shows the url as:

http://localhost/api/values

This happens due to the default controller under Controller Section named as “ValuesController.cs”. Here all the methods that are required to query the API and send it back to the user are handled here. The following section of the code in the ValuesController.cs file prints the output as shown in the image above on the browser. You can explore further methods in this file.

// GET api/values
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
return new string[] { “value1”, “value2” };
}

api/values — here “values” is by default taken as lowercase value of the Controller name — ValuesController.cs. If you create a Controller as TestController.cs, then the urls need to be appended as api/test, so on.

Now, we need to build a controller for the API to handle queries for Students Information linked to Students.db in SQLite. Let’s do that…

In Solution Explorer, under the WebApi Project, right-click on Controllers and choose “Add Controller” option. Then choose the “API Controller with actions”, using Entity Framework.

Choose the Model Class (StudentsInfo) and Data Context Class (DBInteractor) as we have created earlier.

The Controller Name will be automatically generated.

Add API Controller with actions, using Entity Framework

Open the StudentsInfoesController.cs file, and check the following code:

// GET: api/StudentsInfoes
[HttpGet]
public async Task<ActionResult<IEnumerable<StudentsInfo>>> GetStudents()
{
return await _context.Students.ToListAsync();
}

In the browser, if we replace api/values with api/StudentInfoes, we will get the available data from Students.db as follows:

If we provide the url as api/StudentInfoes/1, it will show the record where Id=1

So, that’s it! We have successfully built an API which can read data from SQLite database and return it to the user.

I will soon come up with next section on how to use POSTMAN to query the data using GET and POST methods which will enable us to add, read, update and delete records from the database using the above Web API.

Thanks for your patience for reading and trying to implement till end!

Do post any issues that might come up during the process. Feedback and suggestions to improve — most welcome!

<< Previous

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade