In my previous post we had discussed about .Net core introduction and why we should use it and also how setup your development environment for developing application in .NET Core. Click here to the post first.
In this post I will explain, how to create your first application in .Net core.
First step to run the following command in the terminal to verify installation of the .Net Core:
dotnet --version
Creating a New .NET Core Application
In the Visual Studio, it’s very simple, open you Visual Studio and just click on Create New Project and choose your project type and start the coding, like below screenshot:
But here I will explain how to use the .NET CLI to create a new console application:
Step 1: Run the following command in your terminal
dotnet new console -n MyFirstApp
Step 2: Navigate to the newly created folder:
cd MyFirstApp
Step 3: Writing and Running Your First Application
Open Program.cs and modify it:
using System; class Program { static void Main() { Console.WriteLine("Hello, .NET Core!"); } }
Step 4: Run the application:
dotnet run
You should see Hello, .NET Core in the terminal.
Understanding .NET Core Basics
Key Features:
- Lightweight and modular architecture
- Supports dependency injection
- Built-in middleware support for web applications
Building a Simple Web API with ASP.NET Core
From the Visual Studio, it’s very simple, just click on Create New Project and select ASP.Net Core Web API from the project template as shown in below screenshot
But here I will show how to Create a new Web API project .Net CLI:
Step 1: Run the following command in your terminal
dotnet new webapi -n MyFirstAPI
Step 2: Navigate to the project folder and run:
cd MyFirstAPI dotnet run
Step 3: Visit you application
Visit https://localhost:5001/weatherforecast to test the API. In your case the URL and port would different.
Working with Entity Framework Core
Install EF Core:
dotnet add package Microsoft.EntityFrameworkCore.Sqlite
Create a model:
public class Product { public int Id { get; set; } public string Name { get; set; } }
Debugging and Testing Your Application
Use breakpoints in Visual Studio or Console.WriteLine() for debugging. For testing, create a test project:
dotnet new xunit -n MyFirstTests
Publishing and Deploying Your .NET Core Application
Publish your project:
dotnet publish -c Release -o out
Deploy to Azure using: for publishing on Azure first you need to setup your account on azure, Click here to see how to create Azure account
az webapp up --name MyDotNetApp
Best Practices for .NET Core Development
- Use dependency injection effectively, I have written detailed article about the Dependency Injection please click to read to clear your concept about the Dependency Injection
- Follow MVC architecture for maintainability
- Optimize performance and security
In conclusion, you’ve successfully created and run your first .NET Core application! Keep exploring more advanced topics like authentication, microservices, and cloud deployment. I will write about these advanced topic very soon and post over here.
Thank you for reading this article, I hope you find it useful, if you have any questions, please feel free to write in comment box, I love to reply.
Here are some FAQs about the post, which may be useful
FAQs about the post
Question 1: What is .NET Core best used for?
.NET Core is ideal for web applications, APIs, microservices, and cross-platform development.
Question 2: How does .NET Core differ from .NET Framework?
.NET Core is cross-platform, lightweight, and open-source, while .NET Framework is Windows-only.
Question 3: Can I use .NET Core for desktop applications?
Yes, using Windows Forms or WPF with .NET Core 3.0+.
Question 4: Is .NET Core open-source?
Yes, Dotnet Core is Open source and it is available on GitHub for contributions.
Question 5: How do I keep my .NET Core application updated?
You can keep .Net Core application updated by regularly updating the SDK and NuGet packages using:
dotnet --info dotnet restore