Helllo guys am trying to build my first ASP.NET application here and I have a Models folder and Controllers folder. The models folder contains the class for an entity called customer whose properties are to be read from sqlite database. Thee is also a Pages folder that contains a folder called Shared that contains files such as index.cshtml. When I launch the project, a web browser opens up with the later file opened. The controllers folder contains files such as DataBaseController with the code to query the database and return a response in the specified url. I am trying to access the DataBaseController file directly from the url section like localhost:5001/api/databases but it is returning 404 Not Found. Please help me access the controller file route rom the browser. Below is the DatabaseController.cs file. How do I get the url to show the results of the controller below thank you.

using Databases.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Data.Sqlite;

namespace SqliteFromScratch.Controllers
{
    // MVC is handling the routing for you.
    [Route("api/[Controller]")]
    public class DatabaseController:Controller
    {
        [HttpGet]
        //change the return type to return the 
        //customer lists from the database
        public List<Customer> GetData()
        {
            //initialize the list
            List<Customer> data = new List<Customer>();
            //perform a query using the connection for the database file
            string dataSource = "Data Source=" + Path.GetFullPath("chinook.db");
            using (SqliteConnection conn = new SqliteConnection(dataSource))
            {

                conn.Open();

                // sql is the string that will be run as an sql command
                string sql = $"select * from customers limit 200;";
                using (SqliteCommand command = new SqliteCommand(sql, conn))
                {

                    // reader allows you to read each value that comes back and do something to it.
                    using (SqliteDataReader reader = command.ExecuteReader())
                    {

                        // Read returns true while there are more rows to advance to. then false when done.
                        while (reader.Read())
                        {

                            // map the data to the model.
                            // add each one to the list.
                            Customer customer = new Customer()
                            {
                                Id = reader.GetInt32(0),
                                FirstName=reader.GetString(1),
                                LastName=reader.GetString(2),
                                Company=reader.GetString(3),
                                Address=reader.GetString(4),
                                City=reader.GetString(5),
                                State=reader.GetString(6),
                                Country=reader.GetString(7),
                                PostalCode=reader.GetString(8),
                                Phone=reader.GetString(9),  
                                Fax=reader.GetString(10),
                                Email=reader.GetString(11), 
                                SupportRep=reader.GetInt32(12),
                            };
                            //add each one to the list
                            data.Add(customer);
                        }
                    }
                }
                conn.Close();

            }
            return data;
        }
    }
}

[Route("api/[Controller]")]

Doesn't this have to be:

[Route("api/databases")]

for your URL to work?

Okay, trying that out in a moment, though I found this on the web , its the programmati way of mapping the route to a return string?

app.MapGet("/api/databases", () =>new DatabaseController().GetData());

On the program.cs file?

Not sure. Only used the [Route()] method and the way of the RouteConfig.cs

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.