Create a folder.
$ mkdir SampleDotNet
$ cd SampleDotNet
Create a solution file.
$ dotnet new sln
Create a web api, mvc or other type of project. See the list of available project templates for the dotnet new
command.
$ dotnet new webapi -n SampleDotNet.Api
Add the project to the solution.
# Add API to solution
$ dotnet sln add SampleDotNet.Api/SampleDotNet.Api.csproj
Create a class library project.
$ dotnet new classlib -n SampleDotNet.Services
Reference the library project from the web api project.
# Add class library to solution
$ dotnet sln add SampleDotNet.Services/SampleDotNet.Services.csproj
# Reference class library from API project
$ dotnet add SampleDotNet.Api/SampleDotNet.Api.csproj reference SampleDotNet.Services/SampleDotNet.Services.csproj
Create an .editorconfig file containing styling and coding standards.
# Create a default .editorconfig file
$ dotnet new editorconfig
Enforce the code style on build by adding the following line to your .csproj
file.
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>1.0.0</Version>
+ <EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
</PropertyGroup>
Now you'll see all the relevant warnings on build. You can also escalate those warnings to errors so that your build fails if proper coding conventions aren't followed. See ESLint your C# in VS Code with Roslyn Analyzers for more info.
Finally, you can format your code on build to ensure formatting conventions are followed. Add this to your .csproj
.
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<Version>1.0.0</Version>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
</PropertyGroup>
+ <Target Name="Format" BeforeTargets="Build">
+ <Message Importance="High" Text="Formatting code: dotnet format"></Message>
+ <Exec Command="dotnet format" />
+ </Target>
</Project>
Important: Complete the following in your project folder (same folder as .csproj
file), not solution folder.
Install tailwind postcss and autoprefixer.
$ pnpm install tailwindcss @tailwindcss/vite postcss
Create a postcss.config.js
file.
export default {
'@tailwindcss/postcss': {}
}
Add Tailwind directive to your primary css file (probably wwwroot/css/site.css
).
+ @import "tailwindcss";
Add two npm scripts - one to build tailwind for production and a second to watch for changes and recompile in development.
package.json
{
...
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
+ "css:build": "pnpm dlx @tailwindcss/cli --minify -i ./wwwroot/css/site.css -o ./wwwroot/css/site.min.css",
+ "css:dev": "pnpm dlx @tailwindcss/cli --minify -i ./wwwroot/css/site.css -o ./wwwroot/css/site.min.css --watch"
},
...
}
Update your Views\Shared\_Layout.cshtml
to reference the compiled and minified css that Tailwind will create.
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"]</title>
- <link rel="stylesheet" href="~/css/site.css" asp-append-version="true" />
+ <link rel="stylesheet" href="~/css/site.min.css" asp-append-version="true" />
</head>
Finally, update your .csproj
file to build your CSS.
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<Version>1.0.0</Version>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
</PropertyGroup>
...
+ <!-- Consider these css-related files when building -->
+ <ItemGroup>
+ <UpToDateCheckBuilt Include="wwwroot/css/site.css" Set="Css" />
+ <UpToDateCheckBuilt Include="postcss.config.js" Set="Css" />
+ </ItemGroup>
+ <!-- Build CSS on project build -->
+ <Target Name="Tailwind" BeforeTargets="Build">
+ <Message Importance="High" Text="Building CSS"></Message>
+ <Exec Command="pnpm run css:build" />
+ </Target>
</Project>
When developing, tell Tailwind to watch and build CSS anytime there's a change.
$ pnpm run css:dev
Taken from Install Tailwind CSS using PostCSS.