Route Parameters in ASP.NET Core MVC

This project will demonstrate how to use Route Parameters in ASP.NET Core MVC

  • LearnASPNETCoreMVCWithRealApps
    • Controllers
      • DemoController.cs
    • Views
      • Demo
        • Index.cshtml
        • Demo2.cshtml
        • Demo3.cshtml
    • _ViewImports.cshtml
    • Startup.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;

namespace LearnASPNETCoreMVCWithRealApps.Controllers
{
    [Route("demo")]
    public class DemoController : Controller
    {
        [Route("")]
        [Route("index")]
        [Route("~/")]
        public IActionResult Index()
        {
            return View();
        }

        [Route("demo2/{id}")]
        public IActionResult Demo2(int id)
        {
            ViewBag.id = id;
            return View("Demo2");
        }

        [Route("demo3/{id1}/{id2}")]
        public IActionResult Demo3(int id1, string id2)
        {
            ViewBag.id1 = id1;
            ViewBag.id2 = id2;
            return View("Demo3");
        }

    }
}
@{
    Layout = null;
}

<!DOCTYPE html>

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

</body>
</html>
@{
    Layout = null;
}

<!DOCTYPE html>

<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>
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Demo 2</title>
</head>
<body>

    <h3>Demo 3 View</h3>
    Id 1: @ViewBag.id1
    <br />
    Id 2: @ViewBag.id2

</body>
</html>
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;

namespace LearnASPNETCoreMVCWithRealApps
{
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseStaticFiles();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Demo}/{action=Index}/{id?}");
            });

        }
    }
}

Screenshots