Latest Posts
Tuesday, August 3, 2021
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 (
Name
)
VALUES
(
'Socrate'
),
(
'Plato'
),
(
'Spock'
),
(
'Thanos'
);
So your Values table should look like this when you executed the insert query
Thursday, July 29, 2021
In this post we are going to create our first Entity Framework migration and creating an actual database in MySQL.
In order to do that we need to add a few NuGet packages to our Asp.Net Core project including the Entity Framework Core package. But, before we do that we want to find out what what version of .NET Core we are running. Obviously I have it memorized, but for the rest of you, you can type the command dotnet --version to figure out what the version is :) It's always a good idea to try and match the package version with the .NET Core runtime you have. It's one of the more annoying thing with Angular and Asp.Net Core, it's changing constantly. So as you can see my version 3.1.xxx so I should try to install packages that are made for 3.1.x.
The first package we are going to install is the Microsoft.EntityFrameworkCore. So open the ACMEBank.API project with Visual Studio Code. Press Ctrl+Shift+P and type NuGet and select
Thursday, July 8, 2021
Before we run our Asp.Net Core application for the first time we are going to copy a controller from Asp.Net Core 2.2 to help us create the skeleton for our application. A skeleton are the plumbings we do at the beginning of our application to make sure all the mechanisms work. It's called the ValuesController which has been replaced with a more complicated WeatherForecastController. The ValuesController is just a lot easier to work with to get things set up.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
namespace ACMEBank.API.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
// GET api/values
[HttpGet]
public ActionResult> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/values/5
[HttpGet("{id}")]
public ActionResult Get(int id)
{
return "value";
}
// POST api/values
[HttpPost]
public void Post([FromBody] string value)
{
}
// PUT api/values/5
[HttpPut("{id}")]
public void Put(int id, [FromBody] string value)
{
}
// DELETE api/values/5
[HttpDelete("{id}")]
public void Delete(int id)
{
}
}
}
Tuesday, June 29, 2021
In this post we are going to use the .NET Core CLI to create a web api application for our app. The first thing to do is create an a folder to hold our files. Since we are using Linux you want to sudo su into the terminal and create the following folder /opt/app/ACMEBank. Once that's done cd into it and you are ready to type in the command to create the Web Api.
Here are the steps:
1. What you want to do first is to make sure that you have .NET Core install by typing in dotnet --info, there should be information on your .NET Core install. If you don't have .NET install you can follow the directions in this post.
Tuesday, September 17, 2019
Here are steps to create our first Api:
1. First we want to set up our application to use postman to test out our api. To do that right-click on the project and click on "Properties", in Properties screen click on "Debug" and uncheck "Launch browser" and make a note of the port number of the App URL. We will make the api calls with postman initially to make sure that our api works. Postman has become pretty popular with api development because it allows us to make api calls and see the results. You can get postman here
https://www.getpostman.com/downloads/ I would get the desktop version because it's more robust.
2. Now we are ready to create a ProductController to be our api controller, Asp.NET Core has the api control built-in so we don't have to do anything special for a controller to be a web api controller. So create a file call ProductController in the Controllers folder. The ProductController should have the following code
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using NorthwindCafe.Web.Data;
using System;
namespace NorthwindCafe.Web.Controllers
{
[Route("api/[Controller]")]
public class ProductController : Controller
{
private readonly IProductRepository _repository;
private readonly ILogger _logger;
public ProductController(IProductRepository repository, ILogger logger)
{
_repository = repository;
_logger = logger;
}
[HttpGet]
public IActionResult Get()
{
try
{
return Ok(_repository.GetProducts());
}
catch (Exception ex)
{
_logger.LogError($"Get products failed {ex}");
return BadRequest("Get products failed");
}
}
}
}
The code above is pretty straightforward. You have the define private variables _repository and _logger for the repository to hold the injected objects in the constructor. Then you define a method call Get with the decorator [HttpGet] to handle get requests. The Get() method returns the list of products and retruns Ok = 200 status code if everything is ok and returns a 400 error status code if there's an exception. The other important thing is the [Route("api/[Controller]")] decorator. This is what you typed into the browser. So for this route you would type http://localhost:50051/api/product into Postman.
3. So now we are ready to test our code in Postman, first we need to run our code, press CRTL+F5
Then select "GET" method on Postman call, type in the URL localhost:50051/api/product and you will see the list of products returned in Json, usually the resource in this product should be pluralize, but I forgot the s. So the URLs should be localhost:<port>/api/products but since it's just development we can let it slide. However, if you work with other people you might want to pluralize it.





