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;
namespace LearnASPNETCoreMVC5.Controllers
{
[Route("demo")]
public class DemoController : Controller
{
[Route("")]
[Route("index")]
[Route("~/")]
public IActionResult Index()
{
return View();
}
[Route("demo2")]
public IActionResult Demo2([FromQuery(Name = "id")] string id)
{
ViewBag.id = id;
return View("Demo2");
}
[Route("demo3")]
public IActionResult Demo3([FromQuery(Name = "id1")] int id1, [FromQuery(Name = "id2")] string id2)
{
ViewBag.id1 = id1;
ViewBag.id2 = id2;
return View("Demo3");
}
[Route("demo4")]
public IActionResult Demo4()
{
var id1 = int.Parse(HttpContext.Request.Query["id1"].ToString());
var id2 = HttpContext.Request.Query["id2"].ToString();
ViewBag.id1 = id1;
ViewBag.id2 = id2;
return View("Demo4");
}
}
}
Create View
Create new folder named Views. In this folder, create new folder named Demo and add new views as below:
Index View
In Demo folder, create new view named Index.cshtml as below:
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<h3>Index View</h3>
<a asp-controller="demo" asp-action="demo2" asp-route-id="123">Demo 2</a>
<br />
<a asp-controller="demo" asp-action="demo3" asp-route-id1="123" asp-route-id2="p01">Demo 3</a>
<br />
<a asp-controller="demo" asp-action="demo4" asp-route-id1="123" asp-route-id2="p01">Demo 4</a>
</body>
</html>
Demo2 View
In Demo folder, create new view named Demo2.cshtml as below:
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Demo 2</title>
</head>
<body>
<h3>Demo 2 View</h3>
Id: @ViewBag.id
</body>
</html>
Demo3 View
In Demo folder, create new view named Demo3.cshtml as below:
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Demo 3</title>
</head>
<body>
<h3>Demo 3 View</h3>
Id 1: @ViewBag.id1
<br />
Id 2: @ViewBag.id2
</body>
</html>
Demo4 View
In Demo folder, create new view named Demo4.cshtml as below:
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Demo 4</title>
</head>
<body>
<h3>Demo 4 View</h3>
Id 1: @ViewBag.id1
<br />
Id 2: @ViewBag.id2
</body>
</html>
Create Razor View Imports
Select Views folder and right click to select Add\New Item Menu
Select Web\ASP.NET in left side. Select Razor View Imports item and click Add button to Finish
In _ViewImports.cshtml file and TagHelpers library as below:
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
Structure of ASP.NET Core MVC 5 Project
Run Application
Access Index action in Demo controller with following url: http://localhost:48982
Output
Access Demo2 action in Demo controller with following url: http://localhost:48982/demo/demo2?id=123
Output
Access Demo3 action in Demo controller with following url: http://localhost:48982/demo/demo3?id1=123&id2=p01
Output
Access Demo4 action in Demo controller with following url: http://localhost:48982/demo/demo4?id1=123&id2=p01
Output