Parameters to Routes in ASP.NET Core Razor Pages

On the Visual Studio, create new ASP.NET Core Web Application project

Select Empty Template

Click Ok button to Finish




Open Startup.cs file and add new configurations as below:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;

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

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

            app.UseMvc();
        }
    }
}

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




Create new folder named Pages. In this folder, create new Razor Page named Index as below:

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace LearnASPNETCoreRazorPagesWithRealApps.Pages
{
    public class IndexModel : PageModel
    {
        [BindProperty(Name = "id3", SupportsGet = true)]
        public string Id3 { get; set; }

        [BindProperty(Name = "id4", SupportsGet = true)]
        public int Id4 { get; set; }

        public string Result { get; set; }

        public void OnGet()
        {
        }

        public void OnGetMenu1(string id1)
        {
            Result = "Id1: " + id1;
        }

        public void OnGetMenu2(string id1, int id2)
        {
            Result = "Id1: " + id1;
            Result += "<br>Id2: " + id2;
        }

        public void OnGetMenu3()
        {
            Result = "Id3: " + Id3;
            Result += "<br>Id4: " + Id4;
        }

    }
}
@page
@model LearnASPNETCoreRazorPagesWithRealApps.Pages.IndexModel
@{
    Layout = null;
}

<!DOCTYPE html>

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

    <a asp-page="Index" asp-page-handler="Menu1" asp-route-id1="p01">Menu 1</a> |
    <a asp-page="Index" asp-page-handler="Menu2" asp-route-id1="p02" asp-route-id2="123">Menu 2</a> |
    <a asp-page="Index" asp-page-handler="Menu3" asp-route-id3="p03" asp-route-id4="456">Menu 3</a>
    <br />
    @Html.Raw(Model.Result)

</body>
</html>




Open Index Razor Page with following url: http://localhost:1115

Click Menu 1 link to access OnGetMenu1 method in Page Model with following url: http://localhost:1115/?id1=p01&handler=Menu1

Click Menu 2 link to access OnGetMenu2 method in Page Model with following url: http://localhost:1115/?id1=p02&id2=123&handler=Menu2

Click Menu 3 link to access OnGetMenu3 method in Page Model with following url: http://localhost:1115/?id3=p03&id4=456&handler=Menu3