ASP dotNet Core Empty template
Creating Static Website in dotNet
If you start with the ASP.NET Core Empty template, you can easily turn it into a simple static website by enabling static files and creating HTML, CSS, and JavaScript files.
Step 1: Create a new ASP.NET Core Empty project
dotnet new web -n StaticWebsite
cd StaticWebsite
The project structure looks like:
StaticWebsite
|---Program.cs
|--- appsettings.json
|--- StaticWebsite.csproj
Step 2: Create a wwwroot folder
Create this folder in the project root.
StaticWebsite
|--- wwwroot
|--- index.html
|---- css
|--- style.css
|--- js
|--- script.js
|---Program.cs
Step 3: Modify Program.cs
Replace it with:
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
// Enable serving static files
app.UseDefaultFiles();
app.UseStaticFiles();
app.Run();
What these do
UseDefaultFiles() looks for index.html.
UseStaticFiles() serves files from wwwroot.
Step 4: Create index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>My Static Website</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<h1>Welcome!</h1>
<p>This is a static website hosted by ASP.NET Core.</p>
<button onclick="sayHello()">Click Me</button>
<script src="js/script.js"></script>
</body>
</html>
Step 5: Create css/style.css
body {
font-family: Arial, sans-serif;
margin: 40px;
background: #f4f4f4;
}
h1 {
color: steelblue;
}
button {
padding: 10px 20px;
}
Step 6: Create js/script.js
function sayHello() {
alert("Hello from JavaScript!");
}
Step 7: Run the project
dotnet run
or
dotnet watch run
Open:
https://localhost:5001
or
http://localhost:5000
This approach is ideal for simple landing pages, documentation sites, or portfolios where you only need to serve static HTML, CSS, JavaScript, images, and other assets without using Razor Pages or MVC.