.NET Core-Part 2 : Getting started coding
By: Kelum Sampath Edirisinghe
Tags: Aspnetcore, Web Development, Getting Started, Microsoft, Backend Development

welcome back you to my second article of the ASP.NET Core article collection. In previous article ( https://medium.com/@kelumsampathedirisinghe/net-core-part-1-introduction-3de471fa9cf1), we got some idea about ASP.NET Core. If you haven’t read that yet, i suggest to read that before getting started Coding.

Note : ASP.NET Core framework is different than ASP.NET framework. If you are looking for ASP.NET article, then this is not for you.😊

Download & Install

Before go to coding, we want download and install several tools to our computer to create ASP.NET Core application.

First, we want to download and install the .NET Core SDK :

After install you can check your installed .NET Core version in 3 ways. As the first, we can open cmd and run the below command to check running version.

Dotnet --version

Second method, we can go to the following directory and see the all installed versions.

C:\Program Files\dotnet\sdk

last method, we can run following command on cmd to check all information.

dotnet --info

I hope you have successfully installed SDK to your computer.😊

Now, we want to download and install Visual Studio.

And also we need to install the .NET Core cross-platform development tools.

if you have no idea about add/remove workloads to visual studio, visit below link.

we will walk though some simple steps in creating, running, and testing a new ASP .NET Core Web API.

Project Setup

First we want to open Visual Studio and Create a New Project. when prompt Create a New Project window, choose ASP .NET Core Web Application and click Next.

Give a meaning full project name and click create button.

After creating project, it will ask for starting template for our ASP.NET Core Web Application. For now, we’ll choose API and uncheck all the boxes on the right for the purposes of a simple demo application.

Then click on Create button, Now your project should be created and opened.😊

Running The API Project

Before going to code we need to make sure our API Project has been created properly or not. So, first go to debug menu and click start without debugging. It will take several minutes to launch web page and then display text [“value1”, “value2”] on it. If you can see that text on your opened browser, it means your project successfully created.

If we look at the url of the prompted web browser most probably would be http://localhost:44337/api/values (port may be different). If we look at network tab in inspection, we can see it displayed ( fetched) by a GET API request.

who created this end point, without i write a single piece of code?🤔

okay, let we see inside the created project in visual studio. In solution explorer tab we can see several files and folders are automatically generated. Among those file we can see a file (inside controller folder) named as ValuesController.cs . Inside this file we can see the following code fragments.

This is the endpoint which return [“value1”, “value2”] in initial launching. This file structure was built according to your project template selection.

Okay, Now it’s time to customize auto generated according to our project requirements. 😊

Adding New Project To The Solution

For this example project i would like to add new two library projects in our project solution. first library project is to hold application logic inside the solution and other library project for unit testing.

you can see our solution folder inside solution explorer window. Right click on solution and click Add then click on New Project.

select Class library (.NET Standard) on prompt window and click next. Give a proper name to your new library project (Eg:-TestApp.Starship.Logic).

Then click on create button. Select deficiencies main web API project in solution explorer. Right click on that, select Add and then select Reference. From here, we’ll check the name of the library we added and click OK.

This allows the main API project to use code defined in the library, which helps separate the API-specific logic from the domain logic and makes it easier to port the application logic over to a console, desktop, or mobile application if that is ever needed.

To create test project library, right click on solution explorer and select new project like as previous section. In here, there are two type of test methods called XUnit Test and NUnit test. In this article, we will implement NUnit test. So, select NUnit Testing template. Use proper name to name your projec (Eg:- TestApp. Starship.Tests).

Next we’ll right click on the test project and add dependencies like we did above. This time we’ll add a dependency to both the library and the web API application. This way our tests can invoke methods directly on the controller for testing.

Hope this article is getting little bit longer. So, i planed to write rest of the steps (Adding classes to the library, Creating our first controller, Testing it in the browser, Testing it via code) in next article of this series. Hope you set-up and run our First .NET Core app successfully. Stay tuned for next article to complete our Web API project. Happy Coding!😊