ASP.NETCoreProjectFile
ASP.NETCoreProjectFile
If you have worked with the previous versions of ASP.NET Framework, then you may know
that while creating a Project in Visual Studio, it creates a project file for us. If we use C# as
the programming language, it will create the project file with the “.csproj” extension.
Similarly, if we use VB as the programming language, it will create the project file with
the “.vbproj” extension. However, with ASP.NET Core (.NET), the project file’s format and
content have changed significantly. Let us understand what changes are made to the
ASP.NET Core Project file with an example.
In previous versions of the ASP.NET Framework, we needed to follow the steps below to
edit the project file.
However, with ASP.NET Core (.NET), we can edit the project file without unloading the
project.
To edit the ASP.NET Core project file, right-click on the project name in the Solution
Explorer and then select Edit Project File from the context menu, as shown in the image
below.
Once you click on the Edit Project File then the Project file will open in the Visual Studio
editor as shown in the below image.
Understanding the ASP.NET Core Project File:
<Project Sdk=”Microsoft.NET.Sdk.Web”>
This element defines the project and specifies that it uses the Microsoft.NET.Sdk.Web SDK,
which is used for building web applications, including ASP.NET Core applications. The SDK
includes the necessary tools and libraries for web development.
<PropertyGroup>
The <PropertyGroup> element is used to define a group of properties that control various
settings and configurations for the project. This specific PropertyGroup contains three
properties:
The ASP.NET Core Framework follows a modular approach. This means that it will not
include anything, i.e., package references to the project by default. Only the necessary
packages are added by default.
The package reference is added to the application project file whenever we add new
packages to our application. Please have a look at the following Dependencies section of
our project. Whenever we add a package, that package and its dependency packages are
stored here.
Let’s first add a package from the NuGet Package Manager and see what happens. Go
to Tools => NuGet Package Manager => Manage NuGet Packages for
Solution… option from the context menu, as shown in the image below.
Now, let us add the Newtonsoft.json package. Select the browse tab, search for
Newtonsoft, and install it, as shown in the image below.
Once the package is installed successfully, it will add a reference inside the dependencies
section, as shown in the image below.
As we have only added one package, Newtonsoft.json, it added its package references
here. At the same time, it will also add that package reference in the application project
file, as shown in the image below.
Understanding <ItemGroup>
The <ItemGroup> element is used to define a group of items, such as package references,
project references, or other files that should be included in the project. This specific
ItemGroup contains one package reference:
Again, go to the NuGet Package Manager, select the installed tab, select the package, and
uninstall it, as shown in the image below.
Once you delete the package, it should remove the reference from both the dependencies
and the project file.
ASP.NET Core Project file (.csproj) allows us to configure a variety of settings that control
how our application is built, run, and deployed. These settings are defined using XML
elements within the project file. The following are some of the most common settings we
can configure in an ASP.NET Core project file:
SDK Type: <Project Sdk=”…”>: Specifies the SDK used, which defines the tools
and libraries included. Common SDKs for ASP.NET Core include
Microsoft.NET.Sdk.Web for web apps and Microsoft.NET.Sdk for other types of apps.
Target Framework: <TargetFramework> or <TargetFrameworks>: Sets the
version of .NET that the project targets.
Nullable Reference Types: <Nullable>: Enables or disables nullable reference
types, which can help prevent null reference exceptions.
Implicit Usings: <ImplicitUsings>: Automatically includes a set of common using
directives to reduce boilerplate code.
Output Type: <OutputType>: Determines the output of the project (e.g., Exe for
executable, Library for DLL).
Package References: <PackageReference Include=”PackageName”
Version=”VersionNumber” />: Defines NuGet package dependencies for the project.
Assembly Information: <AssemblyName>, <RootNamespace>: Customizes the
output assembly’s name and the default namespace.
Build Configurations: <PropertyGroup Condition=”…”>: Allows for defining
different settings for different build configurations, like Debug or Release.
Language Version: <LangVersion>: Specifies the C# language version to use.
For example:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<OutputType>Exe</OutputType>
<AssemblyName>FirstCoreWebApp</AssemblyName>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>10.0</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
</Project>
The Condition attribute on the <PropertyGroup> element allows us to specify when the
properties inside this group should be applied. The condition is written as an expression
that evaluates to true or false.