Generate Custom C# ASP.NET Core Project Template
When starting a new C# ASP.NET Core
projects in Linux, Windows, or Mac, developers often rely on tools like Visual Studio or JetBrains Rider to generate projects. However, some developers (like me) prefer maximum configuration flexibility.
So In this tutorial, I will guide you through the process of creating a custom C# ASP.NET
Core project template from scratch in Linux, allowing you to tailor your project to your specific needs.
If you are new to the
C#
andASP.NET Core
,this tutorial is not for you
I assumed that you already installed the dotnet core on your computer. So we know that the easiest way to create an ASP.NET Core
project without using an IDE is, using the dotnet
command line tool.
dotnet new webapp
If you are familiar with C# you know this generates a Razor Web App project. ( Resources: You can refer to this link to learn more about dotnet new
commands )
The above image shows all generated files and directories by the command dotnet new webapp
.
Now the task is to create the project from scratch. I divided this into some tasks:
Create a new directory
Create specific files
Test project
Yeah, this is the easiest task. Create a new directory. Just run mkdir NewProject
. Rename NewProject
with a suitable name. ( Tip: Need to learn more about the mkdir command
? Refer this link to Windows, this one to Linux, this one to Mac )
Now, let's create specific files. The first one is .csproj
( a.k.a Build File ) file. In the directory that you created, create a file with the project name.
touch NewProject.csproj
.csproj
file is an XML document. The content of the file should look like this:
<Project>
<ItemGroup>
<Compile Include="Main.cs" />
</ItemGroup>
</Project>
For a custom project, you need to configure this manually.
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<!-- Update Main Source File Name -->
<VariableSourceMain>NewProjectMainFile.cs</VariableSourceMain>
<!-- Custom Executable -->
<VariableBuildExecutable>NewProject</VariableBuildExecutable>
<!-- Update Build Information -->
<VariableBuildVersion>1.0.0.0</VariableBuildVersion>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<PropertyGroup>
<!-- Custom Output Path -->
<OutputPath>Build\Export</OutputPath>
<!-- Custom Base Output Path -->
<BaseOutputPath>Build\Output</BaseOutputPath>
<!-- Custom Intermediate Output Path -->
<IntermediateOutputPath>Build\Debug</IntermediateOutputPath>
<!-- Custom Base Intermediate Output Path -->
<BaseIntermediateOutputPath>Build\Object</BaseIntermediateOutputPath>
</PropertyGroup>
<PropertyGroup>
<EnableDefaultItems>false</EnableDefaultItems>
</PropertyGroup>
<PropertyGroup>
<AssemblyName>$(VariableBuildExecutable)</AssemblyName>
<AssemblyVersion>$(VariableBuildVersion)</AssemblyVersion>
<GenerateAssemblyTitleAttribute>false</GenerateAssemblyTitleAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyDescriptionAttribute>false</GenerateAssemblyDescriptionAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<GenerateAssemblyCopyrightAttribute>false</GenerateAssemblyCopyrightAttribute>
<GenerateAssemblyInformationalVersionAttribute>false</GenerateAssemblyInformationalVersionAttribute>
<GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute>
<GenerateAssemblyFileVersionAttribute>false</GenerateAssemblyFileVersionAttribute>
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
</PropertyGroup>
<ItemGroup>
<Compile Include="Source\$(VariableSourceMain)" />
</ItemGroup>
</Project>
Here I changed the following parts:
Source File Directory into
Source/
Build Directory to
Build/
You can change these parts of your choice. Just change the commented parts in the XML source.
The following image shows what it looks like:
Next, you need to create Source/
directory and NewProjectMainFile.cs
file manually inside the Source/
directory.
Now it's finished. So just run the build
command:
dotnet build
It worked! The Build/
directory contains all build files and the Source/
directory contains all the C# source files.
You can follow these pages to learn about more customization options:
If you have any question, please comment it