'Get the value of /page/2 rather than using /page?id=2

I am using a razor page and want to get the value from the path. What is the correct way to do this using the razor page or do I have to parse the Http URL?

    [HttpGet("{id}")]
    public void OnGet(string id)
    {
        if(id == null)
        {

        }
        else
        {

        }
    }


Solution 1:[1]

Change the razor syntax @page in your .cshtml file like below:

@page "{id?}"

[HttpGet("{id}")] cannot be applied to Razor Pages, remove it in your .cshtml.cs file.

Reference:

https://docs.microsoft.com/en-us/aspnet/core/razor-pages/?view=aspnetcore-6.0&tabs=visual-studio#custom-routes

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rena