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 AppSettings File
Select Project and right click to select Add\New Item Menu
Select Web\ASP.NET in left side. Select App Settings File item and click Add button to Finish
In appsettings.json file and new configurations as below:
{
"Message": "Hello World",
"MyConfigs": {
"Config1": "Value of Config 1",
"Config2": "Value of Config 2",
"Config3": "Value of Config 3"
},
"Logging": {
"IncludeScopes": false,
"Debug": {
"LogLevel": {
"Default": "Information"
}
}
}
}
Create Controller
Create new folder named Controllers. In this folder, create new controller named DemoController.cs as below:
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
namespace LearnASPNETCoreMVC5.Controllers
{
[Route("demo")]
public class DemoController : Controller
{
private IConfiguration configuration;
public DemoController(IConfiguration _configuration)
{
configuration = _configuration;
}
[Route("index")]
[Route("")]
[Route("~/")]
public IActionResult Index()
{
ViewBag.result1 = configuration["Message"];
ViewBag.result2 = configuration["MyConfigs:Config1"];
ViewBag.result3 = configuration["MyConfigs:Config2"];
ViewBag.result4 = configuration["MyConfigs:Config3"];
ViewBag.result5 = configuration["Logging:Debug:LogLevel:Default"];
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>
Result 1: @ViewBag.result1
<br />
Result 2: @ViewBag.result2
<br />
Result 3: @ViewBag.result3
<br />
Result 4: @ViewBag.result4
<br />
Result 5: @ViewBag.result5
</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