ASP.NET Core Project File
In this article, I am going to discuss the ASP.NET Core Project File in detail. Please read our previous article, where we discussed How to Create the ASP.NET Core Web Application using Visual Studio 2022 and .NET 6 and discussed the different project templates available as part of the ASP.NET Core Web Application.
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 you use C# as the programming language, it will create the project file with the “.csproj” extension. Similarly, if you use VB as the programming language, it will create the project file with the “.vbproj” extension. But with ASP.NET Core (.NET), the project file’s format and content have changed significantly.
Note: ASP.NET Core Project files and folders are synchronized with physical files and folders. If you add a new file in the project folder, it will directly reflect in the solution explorer. You don’t need to add it to the project explicitly by right-clicking on the project.
Let us understand what changes are made to the ASP.NET Core Project file with an example. So, let’s create a new ASP.NET Core Empty Project.
Creating a new ASP.NET Core Empty Project:
To create a new ASP.NET Core Application using .NET 6, open Visual Studio 2022, and then click on the Create a new project box as shown in the below image.
Once you click on the Create a new project box, it will open the “Create a new project” window. This window includes different .NET 6 application templates. Here we will create a simple Web application from scratch, so select the ASP.NET Core Empty Project template and click the Next button as shown in the image below.
Once you click on the Next button, it will open the following Configure Your New Project window. Here, you need to provide the necessary information to create a new project. First, give an appropriate name for your project (FirstCoreWebApplication), set the location where you want to create this project, and the solution name for the ASP.NET Core Web application. And finally, click on the Next button as shown in the image below.
Once you click on the Next button, it will open the Additional Information window. Here, you need to select .NET 6.0 as the Framework; you also need to check the Configure for HTTPS and Do not use top-level statements check boxes, and finally, click on the Create button as shown in the below image.
Once you click on the Create button will create a new ASP.NET Core Web Application in Visual Studio 2022 using .NET 6 with the following file and folder structure.
How we can Edit the Project File in Previous Versions of ASP.NET:
In our previous versions of ASP.NET Framework, in order to edit the project file, we need to follow the below steps
- First, we need to unload the project
- Then we need to edit the project file
- Once we edit the file, then we need to save the project file
- Then reload the project.
But with ASP.NET Core (.NET), we can edit the project file without unloading the project.
How we can Edit the ASP.NET Core Project File:
ASP.NET Core 1.0 does not create a .csproj file. Instead, it uses .xproj and project.json files to manage the project. This has changed in ASP.NET Core 2.0. Visual Studio now uses the .csproj file to manage projects. 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 below image.
Once you click on the Edit Project File, the Project file will open in the Visual Studio editor, as shown in the image below.
Understanding the ASP.NET Core Project File:
Let us understand the ASP.NET Core Project File elements.
- TargetFramework: The TargetFramework element in the project file specifies your application’s target framework. To specify the target framework in the project file, it uses something called Target Framework Moniker (TFM). In our example, the application targets the framework net6.0. The net6.0 is the Moniker for .NET 6. If you remember, we created this project with .NET Core 6.
- Nullable: The nullable annotation and warning contexts can be set for a project using the <Nullable> element in your .csproj file. This element configures how the compiler interprets the nullability of types and what warnings are emitted. The value for this element is “enable” and “disable”.
- ImplicitUsings: Implicit usings is a feature in .NET that allows the compiler to automatically add global usings to your projects for common namespaces used in projects of the type you’ve chosen. This can help you get started more quickly in .NET by automatically importing common namespaces like System and System.Linq without you having to declare them somewhere in your projects explicitly. The value for this element are enable and disable.
Adding Packages in ASP.NET Core:
As we already discussed, ASP.NET Core Framework follows modular approaches. It means, by default, it will not include anything, i.e., package references to the project. Only the necessary packages which are required are added by default.
Whenever we add any new packages to our application, that package reference will also be added to the application project file. Please have a look at the following Dependencies section of our project. Whenever we add any packages, that package, and its dependency packages will be stored here.
Let us 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 below image.
Now let us add the Newtonsoft.json package. So, select the browse tab, then search Newtonsoft and install it, as shown in the image below.
Once the package is installed successfully, then you will see it will add the reference inside the dependencies section, as shown in the below image.
We have only added one package, i.e., Newtonsoft.json, so it added that package references here. Along with the same, it will also add that package reference in the application project file, as shown in the below image.
Note: Once you delete that package, it will delete the reference from both the dependencies and the project file.
Deleting Package in ASP.NET Core:
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. The Package Reference element of the project file is used to add the references to all the NuGet Packages which are installed for your application.
Note: The CSPRJ file includes settings related to targeted .NET Core Framework, Nullable, ImplicitUsings, NuGet Package References, etc…
In the next article, I am going to discuss ASP.NET Core Main Method in detail. Here, in this article, I try to explain the ASP.NET Core Project File in detail. I hope you enjoy this article and would like your feedback. Please post your feedback, question, or comments about this ASP.NET Core Project File article.
About the Author: Pranaya Rout
Pranaya Rout has published more than 3,000 articles in his 11-year career. Pranaya Rout has very good experience with Microsoft Technologies, Including C#, VB, ASP.NET MVC, ASP.NET Web API, EF, EF Core, ADO.NET, LINQ, SQL Server, MYSQL, Oracle, ASP.NET Core, Cloud Computing, Microservices, Design Patterns and still learning new technologies.