MVC Website from .NET Core 6 Empty Project
Tags: mvc, netcore6, bootstrap5, website
Published: 2021-05-30 20:33:57


In this article what we are going to do is we will create an Empty .NET Core 6 project and convert it to a full flagged MVC Website using Bootstrap 5.

Open Visual Studio 2019 and create an Empty .NET Core 6 (Preview 4) project.

Select “ASP.NET Core Empty” Template. Select Project Location and click Next.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j42aewdi7530lplpjkp9.png)

On the Next Screen Select Target .Net Framework .NET 6 and Click Create.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tfggfeevc68w06kh4fhu.png)
Once the Project Will be created it will be look like this.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m8xm4zno0a83ih3ksecz.png)
In new template you won’t find Startup.cs file. The only file that exist with code in “Program.cs” file. The content in Program.cs is look like this. 
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5qpvzndz4caavck4rl5y.png)

app.MapGet("/", (Func<string>)(() => "Hello World!")); 

is default line that would be printing in the browser. We would need to remove this. 

Before moving forward we would need to add a nugget package for Razor Pages compilation. So Right Click Project and search for following nugget package and add it.
Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jszv7y2oeisv263mfdg0.png)
Adding nugget package.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k2a4rwg6jsgrjiqy73zj.png)
Next Open the “Program.cs” and add the following line after builder initialization.
builder.Services.AddControllersWithViews();
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6y1epyknm15zu01agb4o.png)

After adding this line we need to add the default router mapping so add the following line before the last line and remove the default “Hellow World” Like.
app.MapDefaultControllerRoute();

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b1xr9s5ielbz13xwk0nt.png)
your Program.cs would look like this.

using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllersWithViews();
await using var app = builder.Build();
if (app.Environment.IsDevelopment()) {
    app.UseDeveloperExceptionPage();
}
app.MapDefaultControllerRoute();
await app.RunAsync();

Now that we have enabled our project for MVC lets move forward and create Models, Views and Controllers Folders.
Your project will look like this.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ecxb21tr01yzd6kctrla.png)

Now, we will add our first controller to the application. Add a controller under the “Controllers” folder and name it as “HomeController.cs”.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t17z3stvwbev9m5g1y9s.png)
Next let’s right click in controller and add Home view in project.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nebbngo9vswxjr8rb36o.png)
Click on Add view and following screen will display.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/95hne6wf38so4ufifvo8.png)
Select “Razor View” and click add.  This will take you to this screen. 
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bjvrs1kf3r72nlx8hp8z.png)
Leave everything as it is and click on Add button.

Our MVC application has been configured. Let’s add Layout page and add Bootstrap in this project.

To add layout page Right Click on View Folder and click on add view button.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3r6b4trru3j0r1hmya8x.png)
This time select “Razor View- Empty” and click Add.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2mv4v0h2ghc0v179oah0.png)
In the next Screen Select “Razor Layout” option.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/syyrmnlxsm1k0ohshgax.png)
Click add and a Layout Page would be added.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/472p8age39oz11jpwi6z.png)
Once the layout Page Added. Add the following line of code in Index page in Home Folder.
Layout = "../_Layout";
Home page would look like this.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wlyuuuoggknr2wpzakhk.png)
Then Open the _Layout Page and add bootstrap CSS CDN in header and JS CDB after the body.
CSS CDN:
"https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css"
JS CDN:
https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/js/bootstrap.bundle.min.js

_Layout page would look like.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9dor1mu5srrdrvlvlx97.png)

Run your application. 

Complete Source Code is available @

https://github.com/itskhurram/MultipageApp

Complete Video can be accessible @

 
[![MVC Website using MVC Core 6 Empty Project with Bootstrap 5](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qwfdhh7u5bun1it0ntsb.png)](https://youtu.be/tkR451bSyiE&feature=youtu.be&hd=1 "MVC Website using MVC Core 6 Empty Project with Bootstrap 5")