Tech Junkie Blog - Real World Tutorials, Happy Coding!: ACME Bank: Step 10: Getting Values From MySQL Database In Asp.Net Core Web Api Controller

Tuesday, August 3, 2021

ACME Bank: Step 10: Getting Values From MySQL Database In Asp.Net Core Web Api Controller

 In the last post we created our ACME Bank database in MySQL, the next step is to get the values from the Values table in the Asp.Net Core WebApi controller.  

Let's create some test data so that we could retrieve the values from the database.  The Values table could contain anything.  So I am going to store famous philosophers throughout history.  These philosophers are so famous that they only have one name: 

You can run the SQL insert statement below to seed the data in MySQL:


INSERT INTO acmebank.Values (

So your Values table should look like this when you executed the insert query

Since the Id column is auto generated we don't need to assign a value.  Now we are ready to retrieve the values in our code.

The first thing we want to do is inject the EntitiesContext that we created in the last post in the ValuesController constructor so that we could use it in the rest of the class. 

With the code below:

       private readonly EntitiesContext _ctx;
        public ValuesController(EntitiesContext ctx)
            _ctx = ctx;

Then we want to change the HttpGet method to retrieve all the values in the table using the Get() method with no parameters. With this code:

       // GET api/values
        public IActionResult Get()
            return Ok(_ctx.Values.ToList());

_ctx is the EntitiesContext that we passed in and IActionResult return object tells Asp.Net Core that the method will return an Http Status Code along with the revelant data. It uses Linq to return the Vvalues entities in a IEnumberable list objet. The default return format is JSON so you don't have to do anything to show JSON to the client.

Now it's time to make another code change to get an individual record with an Id by changing the code of the second HttpGet method. The one that takes an Id as it's parameter.

So the route for this method is something like this http://localhost:5000/api/values/4 and the URL for the first method is http://localhost:5000/values

Change the code for the HttpGet method that takes an Id to this, to retrieve a single value from the database using Linq's FirstorDefault method

        // GET api/values/5
        public IActionResult Get(int id)
            return Ok(_ctx.Values.FirstOrDefault(v => v.Id == id));

The next step is to build the project by pressing Ctrl+Shift+B and selecting build on VSCode

Now open a terminal in the ACMEBank.API folder and type dotnet watch run, to run the application

Now launch Postman and click on the + sign and make a "Get" request by typing in the URL http://localhost:5000/api/values and you should get all the values in the database like the screenshot below.

Now add a /4 to the URL so the URL should be http://localhost:5000/api/values/4 and you should get Thanos back

Previous: Create Our Database In MySQL Using Asp.Net Core And EntityFrameworkCore


  1. Your blog is very informative, finally, I found exactly what I want. Paypal is an excellent service for online payments but lots of its users confront issues while they access Paypal. If you want to resolve your problems then must visit contact Paypal Nederland.

  2. Your blog is very informative and interesting to read, finally, I found exactly what I search for. There are lots of users of Macfee antivirus in the world because of its features and easy interface. If you want to explore more interesting facts about Mcafee antivirus or want to resolve your technical issues then must visit Mcafee ondersteuning nummer.

  3. Excellent post. I was always checking this blog, and I’m impressed! Extremely useful info specially the last part, I care for such information a lot. I was exploring this particular info for a long time. Thanks to this blog my exploration has ended.
    If you want Digital Marketing Serives :-
    Digital marketing Service in Delhi
    SMM Services
    PPC Services in Delhi
    Website Design & Development Packages
    SEO Services PackagesLocal SEO services
    E-mail marketing services
    YouTube plans

  4. This page contains the necessary information I have a enjoyable site if you feel free you can visit my site Yespornplease

  5. This page contains the necessary information The Samsung Galaxy M52 is going to be published very soon if you feel free you can visit my site Samsung M52 Price

  6. I really got into this article. I found it to be interesting and loaded with unique points of interest. I like to read material that makes me think. Thank you for writing this great content

    Love back Solution Specialist
    Lost love spell
    Marriage problems and solutions 
    Love guru 
    Online astrology solution Powerful vashikaran mantra in hindi  Remove effect of Black Magic 

  7. Thank you for taking the time to publish this information very useful! Antonio Vega Quimagro


Search This Blog