Home > Software design >  Get the value of /page/2 rather than using /page?id=2
Get the value of /page/2 rather than using /page?id=2

Time:05-14

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
        {

        }
    }

CodePudding user response:

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

  • Related