Create ASP.NET Core MVC 5 Project
On the Visual Studio, select Create a new project from Get Started
Select ASP.NET Core Web Application
Input Project Name and select Location for new project
Select ASP.NET Core 5.0 Version and select ASP.NET Core Empty Template. Click Create button to finish
Add Configurations
Open Startup.cs file and add new configurations as below:
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace LearnASPNETCoreMVC5
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Demo}/{action=Index}/{id?}");
});
}
}
}
Create Controller
Create new folder named Controllers. In this folder, create new controller named DemoController.cs as below:
using Microsoft.AspNetCore.Mvc;
using System;
namespace LearnASPNETCoreMVC5.Controllers
{
[Route("demo")]
public class DemoController : Controller
{
[Route("index")]
[Route("")]
[Route("~/")]
public IActionResult Index()
{
ViewBag.age = 20;
ViewBag.fullName = "Kevin";
ViewBag.status = true;
ViewBag.price = 4.5;
ViewBag.birthday = DateTime.Now;
return View();
}
}
}
Create View
Create new folder named Views. In this folder, create new folder named Demo. Create new view named Index.cshtml as below:
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
Age: @ViewBag.age
<br />
Full Name: @ViewBag.fullName
<br />
Status: @ViewBag.status
<br />
Price: @ViewBag.price
<br />
Birthday: @ViewBag.birthday.ToString("MM/dd/yyyy")
</body>
</html>
Structure of ASP.NET Core MVC 5 Project
Run Application
Access Index action in Demo controller with following url: http://localhost:48982/Demo/Index
Output