Like A Girl

Pushing the conversation on gender equality.

Code Like A Girl

Quick tips to transition from .Net Framework to .Net Core

Recently I had to work on a .Net Core project and it was my first time using it. The transition from .Net Framework to .Net Core wasn’t very smooth because there were things I used to do in a certain way that I could not do in .Net Core so I needed to read up on the fundamentals of .Net Core before proceeding.

Being the lazy reader that I am and slim timeline I had for the project, I decided to start the project and read on .Net Core at the same time. I needed some quick pointers and I wasn’t really getting those on the go so I thought I should share some quick tips I gathered during my transition.

NB: To start using .net core, you need to install the .net SDK. You can download it from

Project Structure — Looking at the ASP.NET Core project structure, we can see that there are several files in an ASP.NET Framework project that no longer exist in ASP.NET Core like the Global.asax and Web.config files. In ASP.NET Core, the Startup class is an enhanced way to boot up your app and the appsettings.json has replaced the web.config file so all your database connections, keys etc can be stated here.

.Net Core Project Structure

Wwwroot folder for static files — There used to be a “Content” folder where we dropped static files (i.e css, images etc). In .NET Core, the wwwroot is the the actual root of the web app when running on a webserver. All static files should be dropped here.

wwwroot folder

Dependency Injection — This is very important to know when working on .NET Core. It is a design pattern in which a class or object has its dependent classes injected (passed to it by another class or object) rather than create them directly. It helps codes to be loosely coupled and also aids easy testing. There was no built in dependency injection container in .Net Framework unlike .Net Core.

If we take a look at the ConfigureServices method in the Startup.cs class, we will see the IServiceCollection which represents the catalogue of dependencies. Now when calling methods or interfaces on controller constructors, they have to be registered with the IServiceCollection catalogue of resources. See below examples;

I created a simple service and Interface and registered it with the IServiceCollection catalogue.

ICountry Service class
CountryService class

Now, I will register the service with IServiceCollection catalogue. See below.

There are different ways of registering services into the IServiceCollection catalogue. This service lifetime defines the conditions under which a new service instance is created.

Transient lifetime services creates a new instance separately for every object that requires it.

Scoped lifetime services are created once per request ie the same instance is used with a request but different across multiple requests.

Singleton lifetime services are created the first time it is requested and every subsequent request uses that same instance.

Determining which lifetime to use depends on the type of instance you want create for the service.

Saving files to appsettings.json– Keys are stored in the appsettings.json file in a json format. In the web.config we keep keys in this format;

In .NetCore, you can save that key as

You can also group your keys. This is a good way to keep related keys together. It makes your file easy to read and understand.

Reading Key and value pairs from appsettings.json — Keys can be accessed through Dependency Injection pattern. To read the keys, create a model with properties same name as the keys in the appsettings.json.

DocumentDirectories Model

Now, inject the model into the Startup class in the ConfigureServices section see below;

ConfigureServices class in the StartUp.cs file

You can now access these keys from your controllers by injecting the model you created (e.g DocumentDirectories) into your constructor using IOptions.

NB: to use IOptions, you have to add the Microsoft.Extensions.Options library.

You can now call it anywhere by accessing the properties in the model created. See below example.

Another good thing about .Net core is the ability to run from command prompt. You can run, build or create a project from command prompt using the dotnet keyword as long as you have .Net Core installed.

I hope this helps kick start your transition from .Net Framework to .NET Core. Until next time.. Cheers!