Introduction
In this post, we will learn about .NET Core and creating an API Application with Test Repository.
About .NET Core
.NET Core is a free and open-source, managed computer software framework for Windows, Linux, and macOS operating systems. It is a cross-platform successor to. NET Framework.
Basically these are console applications based on the type of project and Hosting methods. It will have additional components.
Main Features
- Supports Cross-platform - Runs on Windows, macOS, and Linux.
- Open-source project supported by Microsoft.
- Compatible with existing .NET Framework, Mono and Xamarin through .NET standard.
Create a .NET core Application
Prerequisites
- Visual Studio 2019 (any edition).
- .NET Core 3 or above.
Create a .NET core empty application in Visual studio, by selecting the below project templates. We can select “ASP.NET core Web Application” template.


Select empty project template, we will add other required files and dependencies later.

Once the project has been created we can see that the“program.cs” and “startup.cs” files will be generated. Both files help to start and load .NET core required modules. It also, they help to add .NET Core Middleware applications.
.NET core pipelines/middleware components are similar to older ASP.NET Handlers and Modules component. For example, .NET core pipelines: Diagnostics, Routing and Authentication.
About the Developer Exception page in .NET Core
In “startup.cs”, we have written the below manual exception code to throw the error.
- app.UseEndpoints(endpoints =>
- {
- throw new Exception("Test Exception");
- //endpoints.MapGet("/", async context =>
- //{
- // await context.Response.WriteAsync("Hello World!");
- //});
- });
If we run the application, we can see the below developer-friendly exception page that will display the error,

This error page is controlled by below code,
- if (env.IsDevelopment())
- {
- app.UseDeveloperExceptionPage();
- }
If its in the Development environment, we can see the above page. We can set the environment variable for our core application in Project Properties-> Debug page,

We don’t need to use “app.UseMvc()” instead we can use “app.UseRouting()” in .NET core 3 onwards. Otherwise, we will get the below error:

Create API controller with Routing
We can create the “Controllers” folder and class file or API controller file and make sure it's inherited from the “ControllerBase” class. We also added the “ApiController” and “Route” attribute which helps to access the API endpoints.

The below code uses the “convention” based API routing. When we add new controllers, we don’t need to change the code. For Example: if we have controller name “EmployeeController” and API endpoint will be access like “api\employee”.
- app.UseEndpoints(endpoints
- {
- endpoints.MapControllers();
- });


Join the conversation! Your thoughts help the community grow.