Support us on YouTube by Subscribing our YouTube Channel. Click here to Subscribe our YouTube Channel

Saturday, 20 March 2021

Thursday, 11 March 2021

Create ASP.NET Core Web Application with Visual Studio Code

In this article, we will see How to create an empty ASP.NET Web Application with .NET CLI and Visual Studio Code. In the previous article of this series, we already covered the below articles. 

I recommend you to go through these articles first if you are new to this series or new to .Net Core Technology.
In order to create the .Net Core application, we must have .Net Core SDK installed in the system. (In this example, I am using .Net Core 3.1). Go to the Microsoft official website in order to download the .Net Core SDK.
Once SDK is installed, Open Visual Studio Code Editor. If you don’t have it already installed in your system, then go to the Microsoft official site in order to download it free of cost. Once downloaded and installed, Go to the Terminal menu, click on New Terminal.
You will see the terminal as shown in the below image.
Change the directory to the location where you want to create your first ASP.NET Core Web Application with .Net CLI. In my case, I already created a folder on the desktop (and named it as Project).
Run the dotnet command in the terminal. If dotnet core is already installed in your system, then you will see the options as shown in the below image.
Run the dotnet --version command in order to check the version of .Net Core SDK installed in your system. In my system, I have .Net Core 3.1.402 Version.
You can use --help in order to check the options and commands in case of any help required. Use the dotnet new command in order to create a new project and pass the short name for the type of project/solution you want to create. You can use the--help in order to the available projects as well as options related to .Net Core CLI.
As you can see, we can create several kinds of projects through .Net Core CLI. In this example, I am going to create an ASP.NET Core Empty project.
Run dotnet new web --name <NAME_OF_PROJECT> in order to create an empty ASP.NET Core web application. On executing the command, ASP.Net Core Empty template will be created followed by the dotnet restore command.
Now change the directory to the project location. In my case, I have created a project i.e. AspNetCoreEmptyWebApp so I am changing the directory through the terminal to that folder as it contains the .csproj file which is required to run the .Net Core project through the CLI. Once the directory is changed, run dotnet run command in the terminal in order to launch the web application.
Once the application is launched successfully, copy or click on the URL on which your application is running. You can see Hello World! on the screen which is basically coming from the startup.cs class.
Now let’s open that project into Visual Studio Code. We can directly open the folder through the File menu and clicking on the open folder menu option or by running “code .” in the terminal as shown in the below image.
On opening the project for the first time, A message is prompted to add the required assets to build and debug the application. Click on Yes. On clicking on Yes, a folder i.e. .vscode will be created in the project.
You can see that our ASP.NET Core project is opened in the Visual Studio Code editor with the structure as shown in the below image. (Please check, Project Structure in ASP.NET Core 3.1 Web Application in case you want to understand the project as well as files structure and its role)
Now, go to the Run menu, click on Run without debugging in order to run the application without debugging it. In a few seconds application is launched with the ports specified in the launchsettings.json file.

In case you want to debug the application, add the breakpoint where you want to test and debug the application.
And then go to the Run menu & then click on the Start Debugging option. Once the application reached the breakpoint, it will be highlighted (in a similar manner as in Visual Studio IDE). You can do step in, step out, add items to the watch window, etc.


I hope this article will help you in getting started with creating and managing the ASP.NET Core web application with .Net CLI and launching & debugging it through Visual Studio Code.
Thanks.


Saturday, 6 March 2021

Project Structure in ASP.NET Core 3.1 Web Application

In this article, we will learn about the Project Structure of the ASP.NET Core 3.1 Web Application. This is the continuation of the ASP.NET 3.1 Tutorials series. I will recommend you to go through the below article of this series if not already gone through that:

As in the previous article, we created an empty ASP.NET Core Project and the structure of the project is as shown in the below image.
Let’s start exploring each folder and file that exists in the Project Structure for ease of understanding.

.csproj File: Right-click on the project and then click on Edit Project File in order to edit the .csproj file (In .NET Framework we can’t see that option until the project is unloaded from the solution explorer and the content of .csproj was quite difficult to understand).
Once clicked on Edit Project File, .csproj file will be opened in Visual Studio as shown below.
As you can see TargetFramework element specifies the target framework of our ASP.NET Core application. Netcoreapp3.1 is the TFM that stands for the Target Framework Moniker. On adding the dependencies through NuGet, it will add another ItemGroup element which contains the details of the Package in PackageReference element.

launchSettings.json: Properties folder contains a file i.e. launchSettings.json file which contains all the information required to launch the application. It contains the profiles through which the application can be run, each profile is mapped to a commandName, applicationUrl on which application is launched, environmentVariables, etc.
As you can see, In the above launch setting we have two profiles. One will launch the application on IIS Express and another through the Kestral Server (used in the case when the application is run through the DotNet CLI or profile is selected through the Visual Studio Launch icon).

appsettings.json: In .Net Framework, we store application-level settings or configuration in the web.config. In the .Net Core framework, appsettings.json will do the same work. All the connection strings, application-related settings are stores in the appsettings.json file in JSON format.

Program.cs: Program.cs file contains the main method which is the entry point for the application. It will create a web host builder and configure the services defined in Startup.cs file.

Startup.cs
: Startup.cs file used to define all services used in the application and configure the HTTP Request pipeline with the use of middleware components. It contains ConfigureServices method where you can register the services, classes with the use of built-in DI Container, and Configure method which is used to configure the HTTP Request Pipeline. Request pipeline is configured with the help of middleware components. We will learn in detail about the middleware component in the up coming article of this series.

wwwroot Folder: A wwwroot folder needs to be created in order to serve and store static files like CSS, Javascript, images, icons, etc. We will see its usage when we learn about the Static Files in the upcoming articles of this series.

Another interesting feature of the ASP.NET Core Project that files in the project are sync with the files in the system in real-time. Let say on adding a file through the file explorer in the project (not through Visual Studio), files created/altered immediately reflected in the Solution Explorer window. If we delete any file through file explorer, the file will be deleted immediately from the solution explorer as well. In the older version of .Net Framework, we need to include and exclude files manually if files added/deleted through the windows explorer window.
I hope this article helps you in understanding the basic Folder/Project architecture of the ASP.Net Core project.
Thanks

Friday, 26 February 2021

Create your first application with ASP.NET Core 3.1

In this article, we will create our first ASP.NET Core application with the Visual Studio 2019 and .NET Core 3.1 version. This article is part of the ASP.NET Core series that I am going to start. In this series of articles, I will try to cover the basics as well as will create a demo application for the learning purpose.

Friday, 29 January 2021

Program to check whether the entered year is Leap Year or Not

In this example, we will learn How to find the year entered by the user is Leap Year or not. This is one of the most common interview questions which can be asked on the fresher level interview round.


I will recommend you to check the below link for Top C# Interview Programs asked during the Interview and Examination.


Let’s Begin:

In order to find the year entered by the user is Leap year or not, we have to check first that the year entered by the user is evenly divisible by 4. If the year is evenly divisible by 4 as well as evenly divisible by 100 then the year entered by the user is not a Leap year. In case if it's not evenly divisible by 100 then it is an example of Leap year. In case, If the year is evenly divisible by 400 then this is an example of the Leap year.

Program:

Console.WriteLine("Enter the year to check whether year entered is Leap year or not?");

//Recieve input year from the user

int yearValue = Convert.ToInt32(Console.ReadLine());

//If year is evenly divisible by 4 as well and not divisible by 100 then year is leap year

//If year is evenly divisible by 400 then year is leap year

//Else not a Leap Year

if ((yearValue % 4 == 0 && yearValue % 100 != 0) || (yearValue % 400 == 0))

{

    Console.WriteLine("{0} is a Leap Year", yearValue);

}

else {

    Console.WriteLine("{0} is not a Leap Year", yearValue);

}

//Using Console.ReadLine() in order to hold the terminal/command prompt

Console.ReadLine();

Preview:





I hope this example will help you in your interview/exam preparation.
Thanks

Saturday, 23 January 2021

Program to Count Vowels and Consonants in a string

One of the popular programs which can be asked by the interviewer during the interview is to count no. of Vowels and Consonants in a string. The letters A, E, I, O, and U are called Vowels and the other letters (except vowels) in the alphabet are known as Consonants.

Friday, 11 December 2020

Subscribe us on YouTube

Subscribe Now

Popular Posts

Contact us

Name

Email *

Message *

Like us on Facebook