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) { } } }
Thursday, July 1, 2021
For those of you who works with Visual Studio, working with C# comes out of the box, but with Visual Studio Code you need to install some extensions from the Marketplace to make it suitable for C# development.
Here are the extensions you need to install:
- C#: This extension by Microsoft gives us IntelliSense, references, code editing and highlighting and debugging support
- C# Extensions: This extension let's you create C# files, like classes and interfaces also some keyboard shortcuts like creating. This one seems to be no longer in development for a long time, but it works fine.
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, June 22, 2021
The most important thing for most modern dynamic application these day is choosing a database to store your data. If you are on a Windows environment then you use SQL Server database that is a fine choice. But if you are on Linux MySQL is a great choice also. In this post we are going to go through how to install MySQL server on our Fedora development machine.
Here are the steps to install MySQL on Fedora Linux:
1. First we need to add the MySQL repository to our machine by tying in the following command in the terminal
sudo dnf -y install https://dev.mysql.com/get/mysql80-community-release-fc32-1.noarch
Tuesday, June 15, 2021
In this post we going to get our development environment with NodeJS, VSCode and Postman
The Urls are the following, if you are developing in Linux you should install the Linux version. All these tools are cross platform so you can develop in either Windows, Linux or MacOS:
.NET Core: https://dotnet.microsoft.com/download
Node.JS: https://nodejs.org/en/
Visual Studio Code: https://code.visualstudio.com/#alt-downloads
Postman: https://www.postman.com/downloads/
For .NET Core make sure you download the "recommended" version and not the preview version. Also with NodeJS, usually it has LTS status.
Installing .NET Core SDK and NodeJS On Fedora
In order to install .NET Core SDK on Fedora open the terminal and type in the following
sudo dnf install dotnet-sdk-3.1 -y
The -y tells Fedora to skip the y/N prompt and just go on with the install
Once the SDK is installed you can verify the install with the command dotnet --version
Tuesday, June 1, 2021
In the next month or so I will start a series of blog posts that will take you from scratch on how to deploy an Angular application that makes API calls from an Asp.Net Core web api backend using Entity Framework Core as it's ORM and MySQL as the database. Most tutorials would end there, but I will take it further and deploy the application on AWS so that it lives on the world wide web. Since I don't want to start from scratch I will be modernizing the ACME bank application that I've started with AngularJS. If you are working with AngularJS you know that it's time to update and upgrade because it's at its' last legs and Angular is the future. Some say the journey is more rewarding than the destination. I hope, you will join me on this journey. Thank you, here is a diagram of what's to come
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.
Wednesday, August 28, 2019
Here are the steps to use log4Net:
1. Right click on your NorthwindCafe.Web project and select "Manage NuGet Packages"
Tuesday, August 27, 2019
Monday, August 26, 2019
Here are the steps to create a product repository for the products in the database:
1. Create a folder in the NorthwindCafe.Web solution call "Data"
2. Create an interface call IProductRepository in the "Data" folder, this will be the interface that we implement our repository from.
The code of the IProductRepository should look like this
using NorthwindCafe.Web.Models; using System.Collections.Generic; namespace NorthwindCafe.Web.Data { public interface IProductRepository { IEnumerable<Product> GetProducts(); } }
Friday, August 23, 2019
Here are the steps to seed the NorthwindCafe database:
1. Create a file call DBInitializer in the NorthwindCafe.Web "Models" folder, in the file type in the following code
using System.Linq; namespace NorthwindCafe.Web.Models { public class DbInitializer { public static void Initialize(NorthwindContext context) { context.Database.EnsureCreated(); if(context.Categories.Any()) { return; } var categories = new Category[] { new Category {Name = "Coffee", Description="Coffee", Products = new Product[] { new Product { Name = "Dark Roast", Description = "Dark Roast", Price = 2.0M } } }, new Category {Name = "Tea", Description="Tea", Products = new Product[] { new Product { Name = "Chai", Description = "Chai", Price = 1.5M } } }, new Category {Name = "Pastry", Description="Pastry", Products = new Product[] { new Product { Name = "Cupcake", Description = "Cupcake", Price = 1.25M } } }, new Category {Name = "Food", Description = "Food", Products = new Product[] { new Product { Name = "Hamburger", Description = "Hamburger", Price = 5.0M } } } }; foreach (var c in categories) { context.Categories.Add(c); } context.SaveChanges(); } } }
Thursday, August 22, 2019
Here are the steps to create your NorthwindCafe database:
1. Open the Startup.cs file, then type the following lines in the ConfigureServices method
var connectionString = Configuration["Data:NorthwindContextConnection"]; services.AddDbContext<NorthwindContext>(options => options.UseSqlServer(connectionString));
The line above gets the connection string from the appSettings.json file that we've created earlier. Then use the AddDbContext method in the services instance. Dependency injection will take care of the plumbing for you. Using lamba expression we tell the Entity Framework to use the Sql Sever provider for Entity Framework core.
Make sure you have the following namespaces in your Startup class
using NorthwindCafe.Web.Models;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.EntityFrameworkCore;
Wednesday, August 21, 2019
Follow the steps below to create the NorthwindContext:
1. Create a class in Models folder call NorthwindContext
Tuesday, August 20, 2019
Monday, August 5, 2019
Friday, August 2, 2019
Follow the steps below to enable tag helpers:
1. Right-click on the "Views" folder and create a "View Imports" file