DEV Community

Khurram Shahzad
Khurram Shahzad

Posted on

MVC Website from .NET Core 6 Empty Project

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

On the Next Screen Select Target .Net Framework .NET 6 and Click Create.
image
Once the Project Will be created it will be look like this.

image
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

app.MapGet("/", (Func)(() => "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
Adding nugget package.
image
Next Open the “Program.cs” and add the following line after builder initialization.
builder.Services.AddControllersWithViews();
image

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
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

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

image
Next let’s right click in controller and add Home view in project.
image
Click on Add view and following screen will display.
image
Select “Razor View” and click add. This will take you to this screen.
image
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
This time select “Razor View- Empty” and click Add.
image
In the next Screen Select “Razor Layout” option.
image
Click add and a Layout Page would be added.
image
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
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

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

Top comments (0)